1/8
Loading...
🌳LCA Problem!
Let's find the common ancestor of 5 and 1!
Loading...
Let's find the common ancestor of 5 and 1!
A company's org chart is given as a tree structure. Given two employees, find their common manager with the lowest rank. (The nearest common ancestor)
root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
3
Common ancestor of 5 and 1: 3 (root). 5 is in left, 1 is in right subtree
root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
5
5 is an ancestor of 4, so LCA is 5 itself