Challenge - 5 Problems
LCA Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of LCA for given nodes in a binary tree
Given the binary tree below and nodes 5 and 1, what is the output of the Lowest Common Ancestor (LCA) function?
DSA C#
class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } } 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(TreeNode root, TreeNode p, TreeNode q) { if (root == null || root == p || root == q) return root; TreeNode left = LCA(root.left, p, q); TreeNode right = LCA(root.right, p, q); if (left != null && right != null) return root; return left != null ? left : right; } var ancestor = LCA(root, root.left, root.right); Console.WriteLine(ancestor.val);
Attempts:
2 left
💡 Hint
The LCA is the lowest node that has both nodes as descendants.
✗ Incorrect
The nodes 5 and 1 are children of 3, so their lowest common ancestor is 3.
❓ Predict Output
intermediateOutput of LCA for nodes in left subtree
Given the same binary tree, what is the output of the LCA function for nodes 6 and 2?
DSA C#
TreeNode ancestor = LCA(root, root.left.left, root.left.right); Console.WriteLine(ancestor.val);
Attempts:
2 left
💡 Hint
Both nodes are in the left subtree of 5.
✗ Incorrect
Nodes 6 and 2 are children of 5, so the LCA is 5.
🔧 Debug
advancedIdentify the error in this LCA implementation
What error does the following LCA function produce when called with nodes 5 and 1?
DSA C#
TreeNode LCA(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) return null;
if (root == p) return p;
if (root == q) return q;
TreeNode left = LCA(root.left, p, q);
TreeNode right = LCA(root.right, p, q);
if (left != null && right != null) return root;
if (left != null) return left;
if (right != null) return right;
return null;
}
var ancestor = LCA(root, root.left, root.right);
Console.WriteLine(ancestor.val);Attempts:
2 left
💡 Hint
Check if the function handles base cases correctly.
✗ Incorrect
The function correctly returns the LCA node 3 without errors.
❓ Predict Output
advancedOutput of LCA when one node is ancestor of the other
What is the output of the LCA function for nodes 5 and 4, where 4 is a descendant of 5?
DSA C#
root.left.right.left = new TreeNode(7); root.left.right.right = new TreeNode(4); TreeNode ancestor = LCA(root, root.left, root.left.right.right); Console.WriteLine(ancestor.val);
Attempts:
2 left
💡 Hint
If one node is ancestor of the other, LCA is the ancestor node.
✗ Incorrect
Node 5 is ancestor of node 4, so LCA is 5.
🧠 Conceptual
expertTime complexity of the LCA algorithm
What is the time complexity of the standard recursive Lowest Common Ancestor algorithm in a binary tree with n nodes?
Attempts:
2 left
💡 Hint
The algorithm visits each node at most once.
✗ Incorrect
The recursive LCA algorithm visits each node once, so time complexity is O(n).
