Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. code example
Example: lowest common ancestor in a binary tree
#lca in a binary tree
def lca(root,a,b):
if root in (None,a,b):
return root
left_side=lca(root.left,a,b)
right_side=lca(root.right,a,b)
if left_side and right_side:
return root
return left_side or right_side