Gamified challenge · Recursion
The family is a binary tree given as tree (a TreeNode root; examples are in level order with null for a missing child). Every relative has a different number, and the numbers a and b are both in the tree.
Write lca(tree, a, b) that returns the number of their nearest shared ancestor: the lowest person whose descendants include both a and b. A person counts as their own ancestor, so if a is above b the answer is a, and if a == b it is a.
In [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4], lca(tree, 5, 1) is 3, lca(tree, 6, 4) is 5, and lca(tree, 5, 4) is 5.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google