Loading...
Given a BST as a level-order array and two node values p and q, find their lowest common ancestor (LCA). The LCA is the deepest node that is an ancestor of both p and q (a node can be its own ancestor).
6,2,8,0,4,7,9,null,null,3,5 2 8
6
p = 2 and q = 8, we observe that 2 is in the left subtree of the root (6) and 8 is in the right subtree of the root (6), meaning the root (6) is the first common ancestor of both p and q and, being the root, the deepest such ancestor.6 is the root and the first common ancestor of p and q, it is their lowest common ancestor (LCA).