We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Binary Search Tree : Lowest Common Ancestor
Binary Search Tree : Lowest Common Ancestor
Sort by
recency
|
743 Discussions
|
Please Login in order to post a comment
javascript
JAVA : Resolution:
def lca(root, v1, v2): #Enter your code here if v1< root.info and v2 root.info and v2>root.info: return lca(root.right, v1, v2) else: return root
when I use root.data < v1 instead of v1 < root.data 2 test cases are failed, are there any difference to these? the position is just the difference right?
java 8 simplest solution 1. checking if both v1 & v2 less than current node then lca resides in left substree. 2. else if both v1 & v2 greater than current node then lca resides in right substree. 3. if not both previous then lca node is the node where v1 & v2 divide into different subtree or current node data is v1 or v2, so return this node.