Dsa
Trees
Medium
Common Ancestor Finder
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes. The input first contains two integers representing the values of the nodes, followed by the number of nodes and their values in level order. The output should be a single integer representing the value of the LCA node. If either node does not exist, output -1.
Example Input:
4 5
5
3
8
2
4
null
null
7
null
null
null
null
Example Output:
5
Key concepts
treesancestorpath
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now