What is the Lowest Common Ancestor (LCA) in a binary tree?
The Lowest Common Ancestor of two nodes in a binary tree is the deepest (lowest) node that has both nodes as descendants (where a node can be a descendant of itself).
Click to reveal answer
beginner
In the context of LCA, what does it mean if a node is a descendant of itself?
It means that when checking if a node is a descendant of another, the node itself counts as its own descendant. So if the node is one of the two nodes we are finding LCA for, it can be the LCA.
Click to reveal answer
intermediate
What is the base case in a recursive LCA function for a binary tree?
If the current node is null, or if it matches one of the two target nodes, return the current node.
Click to reveal answer
intermediate
How does the recursive LCA function decide the LCA after exploring left and right subtrees?
If both left and right recursive calls return non-null nodes, the current node is the LCA. If only one side returns a non-null node, that node is returned up the recursion.
Click to reveal answer
intermediate
Why does the LCA algorithm work even if one node is ancestor of the other?
Because the algorithm returns the node itself if it matches one of the target nodes, so if one node is ancestor of the other, it will be returned as LCA.
Click to reveal answer
What does the Lowest Common Ancestor represent in a binary tree?
AThe node with the smallest value
BThe deepest node that has both target nodes as descendants
CThe root node of the tree
DThe leaf node closest to the root
✗ Incorrect
The LCA is the deepest node that has both nodes as descendants.
In the recursive LCA algorithm, what should be returned if the current node is null?
ANull
BThe root node
COne of the target nodes
DThe current node
✗ Incorrect
If the current node is null, return null to indicate no target found here.
If the left recursive call returns a node and the right recursive call returns null, what does the LCA function return?
ANull
BThe right node
CThe left node
DThe current node
✗ Incorrect
Return the non-null node from the left side up the recursion.
What happens if one of the target nodes is the ancestor of the other in the LCA algorithm?
AThe ancestor node is returned as LCA
BThe descendant node is returned as LCA
CThe root node is returned as LCA
DNo LCA is found
✗ Incorrect
The algorithm returns the ancestor node because it matches one of the targets.
Which traversal method is implicitly used in the recursive LCA algorithm?
ALevel order traversal
BInorder traversal
CPreorder traversal
DPostorder traversal
✗ Incorrect
The algorithm explores left and right children first, then processes the current node, which is postorder.
Explain how the recursive Lowest Common Ancestor algorithm works in a binary tree.
Think about how recursion explores children and combines results.
You got /4 concepts.
Describe why the Lowest Common Ancestor algorithm correctly handles the case when one node is ancestor of the other.
Consider what happens when the recursion hits a target node.
You got /4 concepts.
Practice
(1/5)
1. What is the Lowest Common Ancestor (LCA) of two nodes in a binary tree?
easy
A. The node that appears first in an inorder traversal
B. The root node of the binary tree
C. The deepest node that is an ancestor of both nodes
D. The node with the smallest value between the two nodes
Solution
Step 1: Understand ancestor concept
An ancestor of a node is any node on the path from the root to that node.
Step 2: Define lowest common ancestor
The lowest common ancestor is the deepest node that is an ancestor of both given nodes.
Final Answer:
The deepest node that is an ancestor of both nodes -> Option C
Quick Check:
LCA = deepest common ancestor [OK]
Hint: LCA is the deepest shared parent node [OK]
Common Mistakes:
Confusing LCA with root node
Choosing node with smallest value
Selecting node based on traversal order
2. Which of the following is the correct base case condition in a recursive function to find LCA in a binary tree?
easy
A. if (root.right == null) return root;
B. if (root.left == null) return root;
C. if (root.value == null) return root;
D. if (root == null) return null;
Solution
Step 1: Identify base case for recursion
When the current node is null, it means we've reached the end of a path without finding the target nodes.
Step 2: Correct base case condition
Return null to indicate no ancestor found in this path.
Final Answer:
if (root == null) return null; -> Option D
Quick Check:
Base case returns null on null node [OK]
Hint: Return null when node is null [OK]
Common Mistakes:
Returning root when left or right child is null
Checking root.value == null which is invalid
Missing base case leading to infinite recursion
3. Consider the following binary tree and code snippet to find LCA of nodes 5 and 1:
3
/ \
5 1
/ \ / \
6 2 0 8
function findLCA(root, p, q) {
if (!root || root === p || root === q) return root;
const left = findLCA(root.left, p, q);
const right = findLCA(root.right, p, q);
if (left && right) return root;
return left ? left : right;
}
const lca = findLCA(root, node5, node1);
console.log(lca.value);
What will be printed?
medium
A. 5
B. 3
C. 1
D. null
Solution
Step 1: Trace recursive calls for nodes 5 and 1
Both nodes 5 and 1 are children of root 3, so recursion finds them in left and right subtrees respectively.
Step 2: Determine LCA based on recursion
Since left and right recursive calls return non-null (nodes 5 and 1), the current root 3 is the LCA.
Final Answer:
3 -> Option B
Quick Check:
LCA of 5 and 1 is 3 [OK]
Hint: If both sides return nodes, root is LCA [OK]
Common Mistakes:
Returning one of the nodes instead of root
Confusing node values with LCA
Ignoring both left and right recursive results
4. The following code attempts to find the LCA but has a bug. What is the error?
function findLCA(root, p, q) {
if (!root) return null;
if (root === p || root === q) return root;
const left = findLCA(root.left, p, q);
const right = findLCA(root.right, p, q);
if (left) return left;
if (right) return right;
return null;
}
medium
A. It returns left child even if right child is also non-null
B. It does not check if root is null
C. It returns root instead of null when no nodes found
D. It does not handle the case when p or q is missing
Solution
Step 1: Analyze return conditions after recursion
The code returns left if left is non-null without checking right, missing the case when both are non-null.
Step 2: Identify missing combined check
It should return root if both left and right are non-null, indicating LCA found at current root.
Final Answer:
It returns left child even if right child is also non-null -> Option A
Quick Check:
Must check both sides before returning [OK]
Hint: Check both left and right before returning root [OK]
Common Mistakes:
Returning left without checking right
Missing combined condition for LCA
Ignoring null root check
5. Given a binary tree where nodes 2 and 4 are in the left subtree of node 3, and nodes 5 and 6 are in the right subtree of node 3, what is the LCA of nodes 2 and 6?
hard
A. 3
B. 2
C. 4
D. 5
Solution
Step 1: Understand tree structure
Nodes 2 and 4 are in left subtree of 3; nodes 5 and 6 are in right subtree of 3.
Step 2: Find common ancestor of nodes 2 and 6
Since 2 is in left subtree and 6 is in right subtree, their lowest common ancestor is the root node 3.
Final Answer:
3 -> Option A
Quick Check:
LCA of nodes in opposite subtrees is root [OK]
Hint: LCA of nodes in different subtrees is their root [OK]
Common Mistakes:
Choosing one of the nodes instead of ancestor
Confusing sibling nodes as LCA
Ignoring tree structure
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.