You are given a pointer to values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2.

Sample Input

         4
       /   \
      2     7
     / \   /
    1   3 6

v1 = 1 and v2 = 7

Sample Output

LCA of 1 and 7 is 4


CODE:

"""
Node is defined as
self.left (the left child of the node)
self.right (the right child of the node)
self.data (the value of the node)
"""
def lca(root , v1 , v2):

    if (v1<root.data and v2<root.data):
        return lca(root.left,v1,v2)
    if (v1>root.data and v2>root.data):
        return lca(root.right,v1,v2)
  #Enter your code here
    return root

results matching ""

    No results matching ""