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
What is the base case when finding LCA recursively in a binary tree?
If the current node is NULL or matches one of the target nodes, return the current node as a potential ancestor.
Click to reveal answer
intermediate
How do you decide the LCA after recursive calls on 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 non-NULL, return that side's result.
Click to reveal answer
intermediate
Why is it important that a node can be a descendant of itself in LCA definition?
Because if one node is ancestor of the other, the LCA is the ancestor node itself, so the function must return that node when found.
Click to reveal answer
beginner
What is the time complexity of finding LCA in a binary tree using a single traversal?
The time complexity is O(n), where n is the number of nodes, because each node is visited once during the recursion.
Click to reveal answer
What should the recursive LCA function return if the current node is NULL?
ANULL
BCurrent node
CLeft child
DRight child
✗ Incorrect
If the current node is NULL, it means no ancestor found in this path, so return NULL.
If the left subtree returns a non-NULL node and the right subtree returns NULL, what does the LCA function return?
ACurrent node
BNULL
CRight subtree result
DLeft subtree result
✗ Incorrect
Only the left subtree has a target node or ancestor, so return that result.
If both left and right recursive calls return non-NULL, what is the LCA?
ALeft child
BRight child
CCurrent node
DNULL
✗ Incorrect
The current node is the lowest node that has both targets in different subtrees.
Can the LCA be one of the nodes itself?
AYes
BNo
COnly if nodes are siblings
DOnly if nodes are leaves
✗ Incorrect
If one node is ancestor of the other, it is the LCA.
What is the worst-case time complexity of the LCA algorithm in a binary tree?
AO(log n)
BO(n)
CO(n log n)
DO(1)
✗ Incorrect
The algorithm visits each node once, so time complexity is linear in number of nodes.
Explain the recursive approach to find the Lowest Common Ancestor in a binary tree.
Think about how recursion explores subtrees and combines results.
You got /5 concepts.
Describe why the LCA can be one of the nodes itself and how the algorithm handles this case.
Consider when one node is above the other in the tree.
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 deepest node that is an ancestor of both nodes
B. The root node of the binary tree
C. The node with the smallest value between the two nodes
D. The node that appears first in an inorder traversal
Solution
Step 1: Understand the definition of LCA
The Lowest Common Ancestor is the deepest node in the tree that has both given nodes as descendants (a node can be a descendant of itself).
Step 2: Differentiate from other nodes
It is not necessarily the root or the node with smallest value, but the common ancestor closest to the two nodes.
Final Answer:
The deepest node that is an ancestor of both nodes -> Option A
Quick Check:
LCA = deepest common ancestor [OK]
Hint: LCA is the lowest node common to both paths [OK]
Common Mistakes:
Confusing LCA with root node
Choosing node by value instead of position
Thinking LCA is the first visited node in traversal
2. Which of the following is the correct C++ function signature to find LCA in a binary tree node structure?
easy
A. int findLCA(int root, int n1, int n2);
B. TreeNode* findLCA(TreeNode* root, int n1, int n2);
C. void findLCA(TreeNode root, int n1, int n2);
D. TreeNode findLCA(TreeNode root, int n1, int n2);
Solution
Step 1: Analyze parameter types
The function should accept a pointer to the root node and two integer values representing node keys.
Step 2: Analyze return type
The function returns a pointer to the TreeNode representing the LCA, so return type must be TreeNode*.
Final Answer:
TreeNode* findLCA(TreeNode* root, int n1, int n2); -> Option B
Quick Check:
Function signature with pointers and correct return type [OK]
Hint: LCA function returns pointer to TreeNode [OK]
Common Mistakes:
Using value instead of pointer for root
Returning int instead of TreeNode*
Passing root by value causing copy
3. Given the following binary tree and code snippet, what will be the output?
#include <iostream>
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
TreeNode* findLCA(TreeNode* root, int n1, int n2) {
if (!root) return nullptr;
if (root->val == n1 || root->val == n2) return root;
TreeNode* left = findLCA(root->left, n1, n2);
TreeNode* right = findLCA(root->right, n1, n2);
if (left && right) return root;
return left ? left : right;
}
int main() {
TreeNode* root = new TreeNode(3);
root->left = new TreeNode(5);
root->right = new TreeNode(1);
root->left->left = new TreeNode(6);
root->left->right = new TreeNode(2);
root->right->left = new TreeNode(0);
root->right->right = new TreeNode(8);
TreeNode* lca = findLCA(root, 6, 2);
std::cout << lca->val << std::endl;
return 0;
}
medium
A. 6
B. 3
C. 2
D. 5
Solution
Step 1: Trace the tree structure and nodes
Nodes 6 and 2 are both children of node 5 in the tree.
Step 2: Apply LCA logic
Since 5 is the lowest node that has both 6 and 2 as descendants, the function returns node 5.
Final Answer:
5 -> Option D
Quick Check:
LCA(6,2) = 5 [OK]
Hint: LCA is ancestor closest to both nodes [OK]
Common Mistakes:
Choosing root 3 instead of node 5
Confusing child nodes as LCA
Returning one of the input nodes incorrectly
4. Identify the bug in the following LCA function and select the correct fix:
TreeNode* findLCA(TreeNode* root, int n1, int n2) {
if (!root) return nullptr;
if (root->val == n1 || root->val == n2) return root;
TreeNode* left = findLCA(root->left, n1, n2);
TreeNode* right = findLCA(root->right, n1, n2);
if (left && right) return root;
if (left) return right;
return right;
}
medium
A. Add a check for root being nullptr after recursive calls
B. Change 'if (left && right) return root;' to 'return nullptr;'
C. Change 'if (left) return right;' to 'if (left) return left;'
D. Change return type to int instead of TreeNode*
Solution
Step 1: Analyze the return statements
The code incorrectly returns 'right' when 'left' is true, which is wrong logic.
Step 2: Correct the return for left subtree
It should return 'left' when 'left' is true and 'right' is false to propagate the found node correctly.
Final Answer:
Change 'if (left) return right;' to 'if (left) return left;' -> Option C
Quick Check:
Return correct subtree pointer [OK]
Hint: Return the non-null subtree pointer correctly [OK]
Common Mistakes:
Returning wrong subtree pointer
Ignoring nullptr checks
Changing return type unnecessarily
5. Given a binary tree where nodes may not contain unique values, which modification is necessary to correctly find the LCA of two nodes identified by their pointers (not values)?
hard
A. Modify the LCA function to compare node pointers directly instead of values
B. Use node values for comparison as usual, ignoring pointer identity
C. Sort the tree nodes before finding LCA to ensure uniqueness
D. Replace the binary tree with a binary search tree for unique values
Solution
Step 1: Understand the problem with duplicate values
If nodes have duplicate values, comparing values is ambiguous and can lead to wrong LCA.
Step 2: Use pointer comparison
Comparing node pointers directly ensures the exact nodes are identified regardless of value duplicates.
Step 3: Modify function accordingly
The LCA function should check if the current node pointer equals either of the target node pointers.
Final Answer:
Modify the LCA function to compare node pointers directly instead of values -> Option A
Quick Check:
Compare pointers, not values, for duplicates [OK]
Hint: Compare node addresses, not values, for duplicates [OK]
Common Mistakes:
Using values for comparison with duplicates
Assuming tree is BST to fix duplicates
Sorting nodes which is unnecessary
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.