Lowest Common Ancestor of a Binary Tree
Given a binary tree represented as an array in level-order and two values `p` and `q`, return the value of their lowest common ancestor (LCA). **Examples:** ```typescript lowestCommonAncestor([3,5,1,6,2,0,8,null,null,7,4], 5, 1) // 3 lowestCommonAncestor([3,5,1,6,2,0,8,null,null,7,4], 5, 4) // 5 ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(n)
- Space complexity: O(h)
Post-order DFS visits each node in the tree once.
Follow-Up Questions Interviewers Ask About Lowest Common Ancestor of a Binary Tree
- How does this general binary tree LCA differ from BST LCA?
Without BST order properties, both left and right subtrees must be searched unconditionally via DFS.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.