1. let visited;let parent;
    2. var lowestCommonAncestor = function(root, p, q) {
    3. visited = new Set();
    4. parent = new Map();
    5. dfs(root);
    6. while (p != null) {
    7. visited.add(p.val);
    8. p = parent.get(p.val);
    9. }
    10. while (q != null) {
    11. if (visited.has(q.val)) {
    12. return q;
    13. }
    14. q = parent.get(q.val);
    15. }
    16. return null;
    17. };
    18. function dfs(root) {
    19. if (root.left != null) {
    20. parent.set(root.left.val, root);
    21. dfs(root.left);
    22. }
    23. if (root.right != null) {
    24. parent.set(root.right.val, root);
    25. dfs(root.right);
    26. }
    27. }