Your question is Binary Tree Problem Solving. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Handshake represents an organization hierarchy as a binary tree for this exercise. Given the tree root and two distinct node values, return the value of their lowest common ancestor, the deepest node that contains both targets in its subtree.
Implement lowest_common_ancestor(root, p, q).
root is either None or a nested dictionary with the shape {"val": integer, "left": node_or_none, "right": node_or_none}.p and q are distinct integer values that occur exactly once in the tree.def lowest_common_ancestor(root, p, q):