0
0
DSA Typescriptprogramming~20 mins

Lowest Common Ancestor in Binary Tree in DSA Typescript - 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 for given nodes in a binary tree
What is the output of the following TypeScript code that finds the Lowest Common Ancestor (LCA) of nodes 5 and 1 in the binary tree?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function lowestCommonAncestor(root: TreeNode | null, p: TreeNode, q: TreeNode): TreeNode | null {
  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;
}

// Construct the tree
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);
root.left.right.left = new TreeNode(7);
root.left.right.right = new TreeNode(4);

const p = root.left; // Node with val 5
const q = root.right; // Node with val 1

const ancestor = lowestCommonAncestor(root, p, q);
console.log(ancestor ? ancestor.val : null);
Anull
B5
C1
D3
Attempts:
2 left
💡 Hint
Think about where nodes 5 and 1 meet first when traversing from the root.
Predict Output
intermediate
2:00remaining
Output of LCA for nodes in same subtree
What is the output of the following TypeScript code that finds the Lowest Common Ancestor (LCA) of nodes 7 and 4 in the binary tree?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function lowestCommonAncestor(root: TreeNode | null, p: TreeNode, q: TreeNode): TreeNode | null {
  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;
}

// Construct the tree
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);
root.left.right.left = new TreeNode(7);
root.left.right.right = new TreeNode(4);

const p = root.left.right.left; // Node with val 7
const q = root.left.right.right; // Node with val 4

const ancestor = lowestCommonAncestor(root, p, q);
console.log(ancestor ? ancestor.val : null);
A5
B2
C3
D7
Attempts:
2 left
💡 Hint
Both nodes are in the right subtree of node 5.
🔧 Debug
advanced
2:00remaining
Identify the error in LCA function
What error will the following TypeScript code produce when trying to find the LCA of nodes 5 and 1?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function lowestCommonAncestor(root: TreeNode | null, p: TreeNode, q: TreeNode): TreeNode | null {
  if (!root) return null;
  if (root.val === p.val) return root;
  if (root.val === q.val) return root;
  const left = lowestCommonAncestor(root.left, p, q);
  const right = lowestCommonAncestor(root.right, p, q);
  if (left && right) return root;
  return left || right;
}

// Construct the tree
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);
root.left.right.left = new TreeNode(7);
root.left.right.right = new TreeNode(4);

const p = root.left; // Node with val 5
const q = root.right; // Node with val 1

const ancestor = lowestCommonAncestor(root, p, q);
console.log(ancestor ? ancestor.val : null);
ANo error, outputs 3
BTypeError: Cannot read property 'val' of null
COutputs null
DStack overflow due to infinite recursion
Attempts:
2 left
💡 Hint
Check how the function compares nodes using val property.
Predict Output
advanced
2:00remaining
Output when one node is ancestor of the other
What is the output of the following TypeScript code that finds the Lowest Common Ancestor (LCA) of nodes 5 and 4, where 5 is an ancestor of 4?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function lowestCommonAncestor(root: TreeNode | null, p: TreeNode, q: TreeNode): TreeNode | null {
  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;
}

// Construct the tree
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);
root.left.right.left = new TreeNode(7);
root.left.right.right = new TreeNode(4);

const p = root.left; // Node with val 5
const q = root.left.right.right; // Node with val 4

const ancestor = lowestCommonAncestor(root, p, q);
console.log(ancestor ? ancestor.val : null);
A3
B4
C5
Dnull
Attempts:
2 left
💡 Hint
If one node is ancestor of the other, the ancestor node is the LCA.
🧠 Conceptual
expert
2:00remaining
Understanding LCA in a binary tree with no parent pointers
In a binary tree where nodes do not have parent pointers, which of the following statements about the Lowest Common Ancestor (LCA) algorithm is TRUE?
AThe LCA algorithm must use recursion or a stack to explore subtrees from the root downwards.
BThe LCA can be found by sorting the node values and picking the middle value.
CThe LCA is always the root node of the tree regardless of input nodes.
DThe LCA can be found by traversing from each node up to the root and comparing paths.
Attempts:
2 left
💡 Hint
Without parent pointers, you cannot move upwards from a node easily.