You are viewing a single comment's thread. Return to all comments →
Java Iterative solution
static Node lca(Node root,int v1,int v2) { if(root==null) return root; while(root!=null) { if(Math.min(v1, v2) <= root.data && Math.max(v1, v2) >= root.data) return root; else if(Math.min(v1, v2) < root.data && Math.max(v1, v2) < root.data) root = root.left; else root = root.right; } return root; }
Java Recurrsive solution
static Node lca(Node root,int v1,int v2) { if(Math.min(v1, v2) < root.data && Math.max(v1, v2) < root.data) return lca(root.left,v1,v2); else if(Math.min(v1, v2) > root.data && Math.max(v1, v2) > root.data) return lca(root.right,v1,v2); else return root; }
Seems like cookies are disabled on this browser, please enable them to open this website
Binary Search Tree : Lowest Common Ancestor
You are viewing a single comment's thread. Return to all comments →
Java Iterative solution
Java Recurrsive solution