Challenge - 5 Problems
LCA Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of LCA function for given nodes
What is the output of the following code when finding the Lowest Common Ancestor (LCA) of nodes 5 and 1 in the binary tree?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function lowestCommonAncestor(root, p, q) { if (!root || root === p || root === q) return root; const left = lowestCommonAncestor(root.left, p, q); const right = lowestCommonAncestor(root.right, p, q); if (left && right) return root; return left ? left : right; } const 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); const p = root.left; // Node with val 5 const q = root.right; // Node with val 1 const lca = lowestCommonAncestor(root, p, q); console.log(lca.val);
Attempts:
2 left
💡 Hint
Think about where the paths to nodes 5 and 1 meet first in the tree.
✗ Incorrect
The LCA of nodes 5 and 1 is the root node 3 because it is the lowest node in the tree that has both 5 and 1 as descendants.
🧠 Conceptual
intermediate1:30remaining
Understanding LCA when one node is ancestor of the other
In a binary tree, if one node is the ancestor of the other, what will the Lowest Common Ancestor (LCA) function return when called with these two nodes?
Attempts:
2 left
💡 Hint
Remember the LCA is the lowest node that has both nodes as descendants (a node can be a descendant of itself).
✗ Incorrect
If one node is ancestor of the other, the ancestor node is the LCA because it contains the descendant node in its subtree.
🔧 Debug
advanced2:00remaining
Identify the error in this LCA implementation
What error will this code produce when trying to find the LCA of nodes 5 and 1?
DSA Javascript
function lowestCommonAncestor(root, p, q) {
if (!root) return null;
if (root === p || root === q) return root;
const left = lowestCommonAncestor(root.left, p, q);
const right = lowestCommonAncestor(root.right, p, q);
if (left && right) return root;
if (left) return left;
if (right) return right;
return null;
}
// Usage with nodes not from the tree
const p = { val: 5 };
const q = { val: 1 };Attempts:
2 left
💡 Hint
Check if the nodes p and q are the same objects as in the tree or just objects with same values.
✗ Incorrect
The function compares nodes by reference (object identity), but p and q are new objects with same values, so the function never finds them and returns null.
❓ Predict Output
advanced2:00remaining
Output of LCA for nodes in left subtree
What is the output of the code when finding the Lowest Common Ancestor (LCA) of nodes 6 and 2 in the binary tree below?
DSA Javascript
class TreeNode { constructor(val, left = null, right = null) { this.val = val; this.left = left; this.right = right; } } function lowestCommonAncestor(root, p, q) { if (!root || root === p || root === q) return root; const left = lowestCommonAncestor(root.left, p, q); const right = lowestCommonAncestor(root.right, p, q); if (left && right) return root; return left ? left : right; } const 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); const p = root.left.left; // Node with val 6 const q = root.left.right; // Node with val 2 const lca = lowestCommonAncestor(root, p, q); console.log(lca.val);
Attempts:
2 left
💡 Hint
Both nodes are in the left subtree of root. Which node is their common ancestor?
✗ Incorrect
Nodes 6 and 2 are children of node 5, so 5 is their lowest common ancestor.
🧠 Conceptual
expert1:30remaining
LCA in Binary Tree vs Binary Search Tree
Which statement correctly distinguishes the Lowest Common Ancestor (LCA) problem in a Binary Tree from that in a Binary Search Tree (BST)?
Attempts:
2 left
💡 Hint
Think about how BST ordering helps find LCA faster.
✗ Incorrect
BST property allows comparing node values to decide direction, reducing search space. Binary Tree lacks ordering, so full traversal is needed.