How do you find the Lowest Common Ancestor in a general Binary Tree?

DSA
Medium
Amazon
102K views

Similar to the BST version but without the ordering property, requiring a different approach.

Why Interviewers Ask This

This tests your ability to adapt algorithms when constraints (like BST ordering) are removed.

How to Answer This Question

Explain the recursive approach: search for p and q in left and right subtrees. If both are found in different subtrees, the root is LCA. If found in one, return that result. Discuss O(N) time complexity.

Key Points to Cover

  • Recursive search in both subtrees
  • Check if current node is one of the targets
  • Combine results from children
  • O(N) time complexity

Sample Answer

Without the BST property, I must search both subtrees recursively. I check if the current node is p or q. Then I search left and right. If both searches return non-null, the current node is the LCA. If only one returns n…

Common Mistakes to Avoid

  • Assuming BST properties hold
  • Returning the first common ancestor found
  • Not handling the base case correctly

Sound confident on this question in 5 minutes

Answer once and get a 30-second AI critique of your structure, content, and delivery. First attempt is free — no signup needed.

Try it free

Related Interview Questions

Browse all 89 DSA questionsBrowse all 155 Amazon questions