|  | 
|  | 1 | +class Solution { | 
|  | 2 | + | 
|  | 3 | +    private TreeNode ans; | 
|  | 4 | + | 
|  | 5 | +    public Solution() { | 
|  | 6 | +        // Variable to store LCA node. | 
|  | 7 | +        this.ans = null; | 
|  | 8 | +    } | 
|  | 9 | + | 
|  | 10 | +    private boolean recurseTree(TreeNode currentNode, TreeNode p, TreeNode q) { | 
|  | 11 | + | 
|  | 12 | +        // If reached the end of a branch, return false. | 
|  | 13 | +        if (currentNode == null) { | 
|  | 14 | +            return false; | 
|  | 15 | +        } | 
|  | 16 | + | 
|  | 17 | +        // Left Recursion. If left recursion returns true, set left = 1 else 0 | 
|  | 18 | +        int left = this.recurseTree(currentNode.left, p, q) ? 1 : 0; | 
|  | 19 | + | 
|  | 20 | +        // Right Recursion | 
|  | 21 | +        int right = this.recurseTree(currentNode.right, p, q) ? 1 : 0; | 
|  | 22 | + | 
|  | 23 | +        // If the current node is one of p or q | 
|  | 24 | +        int mid = (currentNode == p || currentNode == q) ? 1 : 0; | 
|  | 25 | + | 
|  | 26 | + | 
|  | 27 | +        // If any two of the flags left, right or mid become True | 
|  | 28 | +        if (mid + left + right >= 2) { | 
|  | 29 | +            this.ans = currentNode; | 
|  | 30 | +        } | 
|  | 31 | + | 
|  | 32 | +        // Return true if any one of the three bool values is True. | 
|  | 33 | +        return (mid + left + right > 0); | 
|  | 34 | +    } | 
|  | 35 | + | 
|  | 36 | +    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | 
|  | 37 | +        // Traverse the tree | 
|  | 38 | +        this.recurseTree(root, p, q); | 
|  | 39 | +        return this.ans; | 
|  | 40 | +    } | 
|  | 41 | + | 
|  | 42 | +    /*public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { | 
|  | 43 | +        // Stack for tree traversal | 
|  | 44 | +        Deque<TreeNode> stack = new ArrayDeque<>(); | 
|  | 45 | +
 | 
|  | 46 | +        // HashMap for parent pointers | 
|  | 47 | +        Map<TreeNode, TreeNode> parent = new HashMap<>(); | 
|  | 48 | +
 | 
|  | 49 | +        parent.put(root, null); | 
|  | 50 | +        stack.push(root); | 
|  | 51 | +
 | 
|  | 52 | +        // Iterate until we find both the nodes p and q | 
|  | 53 | +        while (!parent.containsKey(p) || !parent.containsKey(q)) { | 
|  | 54 | +
 | 
|  | 55 | +            TreeNode node = stack.pop(); | 
|  | 56 | +
 | 
|  | 57 | +            // While traversing the tree, keep saving the parent pointers. | 
|  | 58 | +            if (node.left != null) { | 
|  | 59 | +                parent.put(node.left, node); | 
|  | 60 | +                stack.push(node.left); | 
|  | 61 | +            } | 
|  | 62 | +            if (node.right != null) { | 
|  | 63 | +                parent.put(node.right, node); | 
|  | 64 | +                stack.push(node.right); | 
|  | 65 | +            } | 
|  | 66 | +        } | 
|  | 67 | +
 | 
|  | 68 | +        // Ancestors set() for node p. | 
|  | 69 | +        Set<TreeNode> ancestors = new HashSet<>(); | 
|  | 70 | +
 | 
|  | 71 | +        // Process all ancestors for node p using parent pointers. | 
|  | 72 | +        while (p != null) { | 
|  | 73 | +            ancestors.add(p); | 
|  | 74 | +            p = parent.get(p); | 
|  | 75 | +        } | 
|  | 76 | +
 | 
|  | 77 | +        // The first ancestor of q which appears in | 
|  | 78 | +        // p's ancestor set() is their lowest common ancestor. | 
|  | 79 | +        while (!ancestors.contains(q)) | 
|  | 80 | +            q = parent.get(q); | 
|  | 81 | +        return q; | 
|  | 82 | +    }*/ | 
|  | 83 | +} | 
0 commit comments