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 ancestor.
Click to reveal answer
intermediate
What is the base case in the recursive approach to find LCA in a binary tree?
If the current node is null, or if it matches one of the two nodes we are searching for, return the current node.
Click to reveal answer
intermediate
How does the recursive LCA algorithm decide the LCA when both left and right recursive calls return non-null nodes?
If both left and right recursive calls return non-null, it means one node is found in the left subtree and the other in the right subtree, so the current node is their Lowest Common Ancestor.
Click to reveal answer
intermediate
What is the time complexity of the standard recursive LCA algorithm in a binary tree?
The time complexity is O(n), where n is the number of nodes in the tree, because in the worst case the algorithm visits every node once.
Click to reveal answer
What does the Lowest Common Ancestor represent in a binary tree?
AThe root node of the tree
BThe deepest node that is an ancestor of both given nodes
CThe node with the smallest value
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 one of the nodes we are searching for?
AReturn the current node
BReturn the root node
CReturn null
DReturn the other node
✗ Incorrect
If the current node matches one of the nodes, it is returned as a potential ancestor.
If the left recursive call returns a node and the right recursive call returns null, what does the LCA algorithm return?
AThe current node
BThe right node
CNull
DThe left node
✗ Incorrect
If only one side returns a node, that node is passed up as the ancestor.
What is the worst-case time complexity of finding LCA in a binary tree?
AO(log n)
BO(1)
CO(n)
DO(n log n)
✗ Incorrect
The algorithm may need to visit every node once, so O(n).
Which of these is NOT true about the Lowest Common Ancestor?
AIt is always the root node
BIt can be one of the nodes itself
CIt is the lowest node with both nodes as descendants
DIt can be found using recursion
✗ Incorrect
The LCA is not always the root; it depends on the nodes' positions.
Explain how the recursive algorithm finds the Lowest Common Ancestor in a binary tree.
Think about how recursion explores subtrees and combines results.
You got /4 concepts.
Describe a real-life analogy to understand the concept of Lowest Common Ancestor.
Imagine tracing back family members to find a shared grandparent.
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 with the smallest value between the two nodes
B. The root node of the binary tree
C. The deepest node that is an ancestor of both nodes
D. The node that is a child of both nodes
Solution
Step 1: Understand the definition of LCA
The Lowest Common Ancestor is the lowest node in the tree that has both given nodes as descendants (a node can be a descendant of itself).
Step 2: Identify the deepest common ancestor
Among all common ancestors, the LCA is the one farthest from the root (deepest).
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 lowest node common to both paths [OK]
Common Mistakes:
Confusing LCA with root node always
Thinking LCA is the node with smaller value
Assuming LCA is a child of both nodes
2. Which of the following is the correct Go function signature to find the LCA of two nodes in a binary tree?
easy
A. func lowestCommonAncestor(root TreeNode, p *TreeNode, q TreeNode) *TreeNode
B. func lowestCommonAncestor(root TreeNode, p TreeNode, q TreeNode) TreeNode
C. func lowestCommonAncestor(root *TreeNode, p TreeNode, q *TreeNode) TreeNode
D. func lowestCommonAncestor(root *TreeNode, p *TreeNode, q *TreeNode) *TreeNode
Solution
Step 1: Understand pointer usage in Go for tree nodes
Tree nodes are usually passed as pointers to avoid copying and to allow modification.
Step 2: Check parameter and return types
The function should accept pointers to TreeNode for root, p, and q, and return a pointer to TreeNode.
Final Answer:
func lowestCommonAncestor(root *TreeNode, p *TreeNode, q *TreeNode) *TreeNode -> Option D
Quick Check:
Use pointers for nodes in Go function signature [OK]
Hint: Use pointers for tree nodes in Go functions [OK]
Common Mistakes:
Passing nodes by value instead of pointer
Mixing pointer and value types in parameters
Returning value instead of pointer
3. Given the following Go code snippet, what will be the output when finding the LCA of nodes 5 and 1?
type TreeNode struct {
Val int
Left, Right *TreeNode
}
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
if root == nil || root == p || root == q {
return root
}
left := lowestCommonAncestor(root.Left, p, q)
right := lowestCommonAncestor(root.Right, p, q)
if left != nil && right != nil {
return root
}
if left != nil {
return left
}
return right
}
func main() {
root := &TreeNode{Val: 3}
root.Left = &TreeNode{Val: 5}
root.Right = &TreeNode{Val: 1}
root.Left.Left = &TreeNode{Val: 6}
root.Left.Right = &TreeNode{Val: 2}
root.Right.Left = &TreeNode{Val: 0}
root.Right.Right = &TreeNode{Val: 8}
lca := lowestCommonAncestor(root, root.Left, root.Right)
fmt.Println(lca.Val)
}
medium
A. 3
B. 5
C. 1
D. 2
Solution
Step 1: Trace recursive calls for nodes 5 and 1
Starting at root (3), left subtree contains node 5, right subtree contains node 1.
Step 2: Both left and right recursive calls return non-nil nodes
Since left and right are both non-nil, root (3) is the LCA.
Final Answer:
3 -> Option A
Quick Check:
LCA(5,1) = 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
Not handling nil checks properly
4. The following Go function is intended to find the LCA of two nodes in a binary tree. What is the bug?
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
if root == nil {
return nil
}
if root.Val == p.Val || root.Val == q.Val {
return root
}
left := lowestCommonAncestor(root.Left, p, q)
right := lowestCommonAncestor(root.Right, p, q)
if left != nil {
return left
}
if right != nil {
return right
}
return root
}
medium
A. Returns root when both left and right are nil instead of nil
B. Does not check for nil root at start
C. Compares node pointers instead of values
D. Does not recurse on left subtree
Solution
Step 1: Analyze return conditions after recursion
If both left and right are nil, the function incorrectly returns root instead of nil.
Step 2: Understand correct base case for no ancestor found
When neither subtree contains p or q, function should return nil, not root.
Final Answer:
Returns root when both left and right are nil instead of nil -> Option A
Quick Check:
Return nil if no ancestor found [OK]
Hint: Return nil if both sides are nil, not root [OK]
Common Mistakes:
Returning root instead of nil when no match
Missing nil check at start
Comparing values instead of pointers incorrectly
5. You have a binary tree where nodes can have duplicate values. How should you modify the LCA function to correctly find the LCA of two given nodes p and q?
hard
A. Compare node values only since duplicates don't affect LCA
B. Compare node pointers instead of node values to identify nodes
C. Use node values and parent pointers to find LCA
D. Return the node with smaller value as LCA
Solution
Step 1: Understand problem with duplicate values
When values are duplicated, comparing values alone can confuse nodes.
Step 2: Use node pointers to uniquely identify nodes
Comparing pointers ensures the exact nodes p and q are found, not just matching values.
Step 3: Modify function to compare pointers (e.g., root == p)
This guarantees correct LCA even with duplicate values.
Final Answer:
Compare node pointers instead of node values to identify nodes -> Option B
Quick Check:
Use pointers, not values, for unique node identity [OK]
Hint: Use pointers to identify nodes uniquely with duplicates [OK]
Common Mistakes:
Comparing only values causing wrong LCA
Assuming duplicates don't affect LCA
Using parent pointers unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.