Find the deepest node that is an ancestor of two given nodes in a binary tree.
Constraints
Example
>>> # Construct the following binary tree: >>> # 3 >>> # / \ >>> # 5 1 >>> # / \ / \ >>> # 6 2 0 8 >>> # / \ >>> # 7 4 >>> root = TreeNode(3) >>> root.left = TreeNode(5) >>> root.right = TreeNode(1) >>> root.left.left = TreeNode(6) >>> root.left.right = TreeNode(2) >>> root.right.left = TreeNode(0) >>> root.right.right = TreeNode(8) >>> root.left.right.left = TreeNode(7) >>> root.left.right.right = TreeNode(4) >>> lowest_common_ancestor(root, 5, 1) 3 >>> lowest_common_ancestor(root, 5, 4) 5 >>> lowest_common_ancestor(root, 6, 8) 3
Recent Submissions
No submissions yet — hit Run Tests to try!
Hints