Lowest common ancestor in any binary tree
root is a binary tree (not necessarily a search tree) with distinct values that contains
both a and b. Write lca(root, a, b) that returns the value of their lowest common
ancestor.
lca([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 5, 1) -> 3
lca([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 5, 4) -> 5
lca([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4], 7, 4) -> 2
Without the search-tree rule you cannot steer; recurse into both sides and let each subtree report whether it contains one of the targets. The first node where both sides report, or that is itself a target with the other below, is the answer.
def lca(root, a, b):
...