0
0
DSA Typescriptprogramming~20 mins

Maximum Width of Binary Tree in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Maximum Width Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Maximum Width Calculation for a Simple Tree
What is the output of the following TypeScript code that calculates the maximum width of a 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 widthOfBinaryTree(root: TreeNode | null): number {
  if (!root) return 0;
  let maxWidth = 0;
  let queue: Array<[TreeNode, number]> = [[root, 0]];
  while (queue.length > 0) {
    const levelLength = queue.length;
    const levelHeadIndex = queue[0][1];
    let lastIndex = 0;
    for (let i = 0; i < levelLength; i++) {
      const [node, index] = queue.shift()!;
      lastIndex = index;
      if (node.left) queue.push([node.left, 2 * (index - levelHeadIndex)]);
      if (node.right) queue.push([node.right, 2 * (index - levelHeadIndex) + 1]);
    }
    maxWidth = Math.max(maxWidth, lastIndex - levelHeadIndex + 1);
  }
  return maxWidth;
}

const root = new TreeNode(1,
  new TreeNode(3,
    new TreeNode(5),
    new TreeNode(3)
  ),
  new TreeNode(2,
    null,
    new TreeNode(9)
  )
);

console.log(widthOfBinaryTree(root));
A4
B3
C5
D2
Attempts:
2 left
💡 Hint
Consider the width as the number of nodes between the leftmost and rightmost nodes at each level, counting nulls in between.
🧠 Conceptual
intermediate
1:30remaining
Understanding the Role of Indexing in Width Calculation
Why does the algorithm assign indices to nodes when calculating the maximum width of a binary tree?
ATo track the horizontal position of nodes including gaps caused by missing children
BTo count the total number of nodes in the tree
CTo determine the depth of the tree
DTo sort nodes by their values
Attempts:
2 left
💡 Hint
Think about how to measure width including empty spaces between nodes.
🔧 Debug
advanced
2:00remaining
Identify the Error in Width Calculation Code
What error will the following TypeScript code produce when calculating the maximum width of a binary tree?
DSA Typescript
function widthOfBinaryTree(root: TreeNode | null): number {
  if (!root) return 0;
  let maxWidth = 0;
  let queue: Array<[TreeNode, number]> = [[root, 0]];
  while (queue.length > 0) {
    const levelLength = queue.length;
    const levelHeadIndex = queue[0][1];
    let lastIndex = 0;
    for (let i = 0; i <= levelLength; i++) {
      const [node, index] = queue.shift()!;
      lastIndex = index;
      if (node.left) queue.push([node.left, 2 * (index - levelHeadIndex)]);
      if (node.right) queue.push([node.right, 2 * (index - levelHeadIndex) + 1]);
    }
    maxWidth = Math.max(maxWidth, lastIndex - levelHeadIndex + 1);
  }
  return maxWidth;
}
ANo error, code runs correctly
BRuntime error due to queue.shift() returning undefined
CSyntax error because of missing semicolon
DIncorrect maximum width due to off-by-one in loop condition
Attempts:
2 left
💡 Hint
Check the loop condition and how many times queue.shift() is called.
Predict Output
advanced
2:00remaining
Maximum Width for a Skewed Binary Tree
What is the output of the following TypeScript code that calculates the maximum width of a skewed 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 widthOfBinaryTree(root: TreeNode | null): number {
  if (!root) return 0;
  let maxWidth = 0;
  let queue: Array<[TreeNode, number]> = [[root, 0]];
  while (queue.length > 0) {
    const levelLength = queue.length;
    const levelHeadIndex = queue[0][1];
    let lastIndex = 0;
    for (let i = 0; i < levelLength; i++) {
      const [node, index] = queue.shift()!;
      lastIndex = index;
      if (node.left) queue.push([node.left, 2 * (index - levelHeadIndex)]);
      if (node.right) queue.push([node.right, 2 * (index - levelHeadIndex) + 1]);
    }
    maxWidth = Math.max(maxWidth, lastIndex - levelHeadIndex + 1);
  }
  return maxWidth;
}

const root = new TreeNode(1,
  new TreeNode(2,
    new TreeNode(3,
      new TreeNode(4),
      null
    ),
    null
  ),
  null
);

console.log(widthOfBinaryTree(root));
A4
B2
C3
D1
Attempts:
2 left
💡 Hint
A skewed tree has only one node per level, so width is always 1.
🧠 Conceptual
expert
1:30remaining
Why Normalize Indices at Each Level in Width Calculation?
In the maximum width calculation algorithm, why do we subtract the index of the first node at the current level from all node indices before calculating children indices?
ATo count the total number of nodes in the tree
BTo sort nodes by their values at each level
CTo prevent integer overflow and keep indices small for large trees
DTo reverse the order of nodes at each level
Attempts:
2 left
💡 Hint
Think about how indices grow as the tree depth increases.