1def lowest_common_ancestor(root, p, q):
2 if not root or root == p or root == q:
3 return root
4
5 left = lowest_common_ancestor(root.left, p, q)
6 right = lowest_common_ancestor(root.right, p, q)
7
8 if left and right:
9 return root
10 return left if left else right