Question bank › Trees
Dsa Trees Medium

Lowest Common Ancestor with Parent References

You are given a binary tree where each node has a reference to its parent. Implement a function to find the lowest common ancestor (LCA) of two nodes in the tree. The LCA is defined as the lowest node that has both nodes as descendants. Input format: The input starts with an integer n, the number of nodes (1 <= n <= 1000). Followed by n lines, each containing the node value and its parent's value. The root's parent will be -1. The next line contains two node values for which to find the LCA. Output format: Print the value of the lowest common ancestor node. Example: Input: 5 1 -1 2 1 3 1 4 2 5 2 2 3 Output: 1

Key concepts

treeslowest common ancestorparent pointers

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
Part of Praxari's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.