Given a Binary tree and two nodes. Need to find the minimum ancestor, no parent nodes given.Each time when I told answer, they modified question little bit or removed some extra storage which I was taking.

class TreeNode(object):
    def __init__(self,val):
        self.value = val
        self.left = None
        self.right = None

    def __repr__(self):
        return str(self.value)
def find_lca(root,n1,n2):
    if not root:
        return None

    if root.value == n1 or root.value == n2:
        return root

    leftLCA = find_lca(root.left,n1,n2)
    rightLCA = find_lca(root.right,n1,n2)

    if leftLCA and rightLCA:
        return root

    return leftLCA if leftLCA else rightLCA

results matching ""

    No results matching ""