Amazon Interview Question

Given a few scenarios, code them and make sure it works.

Interview Answer

Anonymous

May 30, 2017

def lca(root, n1, n2): # Base Case if root is None: return None # If both n1 and n2 are smaller than root, then LCA # lies in left if(root.data > n1 and root.data > n2): return lca(root.left, n1, n2) # If both n1 and n2 are greater than root, then LCA # lies in right if(root.data < n1 and root.data < n2): return lca(root.right, n1, n2) return root