Challenge - 5 Problems
Maximum Width Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Maximum Width Calculation
What is the output of the following C# code that calculates the maximum width of a binary tree?
DSA C#
using System; using System.Collections.Generic; public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { this.val = val; this.left = left; this.right = right; } } public class Solution { public int WidthOfBinaryTree(TreeNode root) { if (root == null) return 0; int maxWidth = 0; Queue<(TreeNode node, int index)> queue = new Queue<(TreeNode, int)>(); queue.Enqueue((root, 0)); while (queue.Count > 0) { int count = queue.Count; int minIndex = queue.Peek().index; int first = 0, last = 0; for (int i = 0; i < count; i++) { var (node, index) = queue.Dequeue(); int currIndex = index - minIndex; if (i == 0) first = currIndex; if (i == count - 1) last = currIndex; if (node.left != null) queue.Enqueue((node.left, 2 * currIndex + 1)); if (node.right != null) queue.Enqueue((node.right, 2 * currIndex + 2)); } maxWidth = Math.Max(maxWidth, last - first + 1); } return maxWidth; } } class Program { static void Main() { TreeNode root = new TreeNode(1, new TreeNode(3, new TreeNode(5), new TreeNode(3)), new TreeNode(2,null, new TreeNode(9))); Solution sol = new Solution(); Console.WriteLine(sol.WidthOfBinaryTree(root)); } }
Attempts:
2 left
💡 Hint
Consider how the indices are assigned to nodes at each level and how width is calculated as last - first + 1.
✗ Incorrect
The maximum width is calculated by tracking the indices of nodes at each level. The widest level here has width 4, including null positions between nodes.
🧠 Conceptual
intermediateUnderstanding Indexing in Maximum Width Calculation
Why do we subtract the minimum index of the current level from all node indices before calculating width in the maximum width of binary tree algorithm?
Attempts:
2 left
💡 Hint
Think about how indices grow exponentially and how subtracting the minimum index affects them.
✗ Incorrect
Subtracting the minimum index at each level resets the starting index to zero, which prevents large numbers and simplifies the calculation of width as last - first + 1.
🔧 Debug
advancedIdentify the Bug in Maximum Width Calculation
What error will the following code produce when calculating the maximum width of a binary tree?
DSA C#
public int WidthOfBinaryTree(TreeNode root) { if (root == null) return 0; int maxWidth = 0; Queue<(TreeNode node, int index)> queue = new Queue<(TreeNode, int)>(); queue.Enqueue((root, 1)); while (queue.Count > 0) { int count = queue.Count; int minIndex = queue.Peek().index; int first = 0, last = 0; for (int i = 0; i < count; i++) { var (node, index) = queue.Dequeue(); int currIndex = index - minIndex; if (i == 0) first = currIndex; if (i == count - 1) last = currIndex; if (node.left != null) queue.Enqueue((node.left, 2 * currIndex)); if (node.right != null) queue.Enqueue((node.right, 2 * currIndex + 1)); } maxWidth = Math.Max(maxWidth, last - first + 1); } return maxWidth; }
Attempts:
2 left
💡 Hint
Check how child indices are calculated relative to parent indices and the starting index.
✗ Incorrect
The indexing for children is off by one because the root starts at 1 but children are calculated as 2 * currIndex and 2 * currIndex + 1, which is inconsistent and causes incorrect width calculation.
🚀 Application
advancedMaximum Width of a Sparse Binary Tree
Given a binary tree where many nodes are missing (null), what will be the maximum width of this tree after running the maximum width algorithm?
DSA C#
TreeNode root = new TreeNode(1, null, new TreeNode(2, null, new TreeNode(3, null, new TreeNode(4))));
Attempts:
2 left
💡 Hint
Consider how the algorithm counts null positions between nodes to calculate width.
✗ Incorrect
The maximum width counts the positions between the leftmost and rightmost nodes at the deepest level, including null gaps, resulting in width 4.
🧠 Conceptual
expertWhy Use a Queue with Indices in Maximum Width Calculation?
Why does the maximum width of binary tree algorithm use a queue that stores pairs of (node, index) instead of just nodes?
Attempts:
2 left
💡 Hint
Think about how the algorithm measures width including empty spaces.
✗ Incorrect
Storing indices allows the algorithm to know the exact horizontal position of nodes, so it can calculate the width including gaps caused by missing nodes.
