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
|
764 Discussions
|
Please Login in order to post a comment
There's no set up code for doing this in Kotlin, so I wrote some. If you use this, you just need to create 'fun findLowestCommonAncestor(v1: Int, v2: Int, root: Node): Int'
class Node: def init(self,info): self.info = info
self.left = None
self.right = None
'''
def lca(root, v1, v2): node=root if node is None: return None if node.info>v1 and node.info>v2: return lca(node.left,v1,v2) elif node.info
Ingenious and simple! Because it's a binary search tree, the left side is always be lower than the right side!
Oddly enough, my solution implemented the same way in Python as in Java failed test cases 2 and 7 only with Python