You are viewing a single comment's thread. Return to all comments →
My iterative approach
public static Node lca(Node root, int v1, int v2) { if (root == null) return null; Set<Node> set = new HashSet<>(); set.add(root); Node node = root; while (node.data != v1) { node = v1 < node.data ? node.left : node.right; set.add(node); } set.add(node); Node n = root; node = root; while (set.contains(node)) { n = node; node = v2 < node.data ? node.left : node.right; } return n; }
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Binary Search Tree : Lowest Common Ancestor
You are viewing a single comment's thread. Return to all comments →
My iterative approach