0
0
DSA Javascriptprogramming~20 mins

Lowest Common Ancestor in Binary Tree in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LCA Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
A3
B5
C1
Dnull
Attempts:
2 left
💡 Hint
Think about where the paths to nodes 5 and 1 meet first in the tree.
🧠 Conceptual
intermediate
1: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?
AThe descendant node
BThe ancestor node
CThe root node of the tree
Dnull
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).
🔧 Debug
advanced
2: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 };
AInfinite recursion causing stack overflow
BThrows TypeError because root.left is undefined
CReturns root node always
DReturns null because p and q are not actual tree nodes
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.
Predict Output
advanced
2: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);
A5
B3
C6
D2
Attempts:
2 left
💡 Hint
Both nodes are in the left subtree of root. Which node is their common ancestor?
🧠 Conceptual
expert
1: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)?
ALCA algorithms are identical for both Binary Tree and BST
BIn Binary Tree, LCA is always the root; in BST, it is always one of the nodes
CIn BST, LCA can be found using node values and tree ordering; in Binary Tree, structure must be traversed fully
DBST does not have a Lowest Common Ancestor concept
Attempts:
2 left
💡 Hint
Think about how BST ordering helps find LCA faster.