Challenge - 5 Problems
Maximum Width Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of width calculation for a simple binary tree
What is the maximum width of the binary tree after running the given code?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { public int widthOfBinaryTree(TreeNode root) { if (root == null) return 0; java.util.Queue<java.util.Map.Entry<TreeNode, Integer>> queue = new java.util.LinkedList<>(); queue.offer(new java.util.AbstractMap.SimpleEntry<>(root, 0)); int maxWidth = 0; while (!queue.isEmpty()) { int size = queue.size(); int minIndex = queue.peek().getValue(); int first = 0, last = 0; for (int i = 0; i < size; i++) { java.util.Map.Entry<TreeNode, Integer> entry = queue.poll(); TreeNode node = entry.getKey(); int index = entry.getValue() - minIndex; if (i == 0) first = index; if (i == size - 1) last = index; if (node.left != null) queue.offer(new java.util.AbstractMap.SimpleEntry<>(node.left, 2 * index + 1)); if (node.right != null) queue.offer(new java.util.AbstractMap.SimpleEntry<>(node.right, 2 * index + 2)); } maxWidth = Math.max(maxWidth, last - first + 1); } return maxWidth; } public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(3); root.right = new TreeNode(2); root.left.left = new TreeNode(5); root.left.right = new TreeNode(3); root.right.right = new TreeNode(9); Solution sol = new Solution(); System.out.println(sol.widthOfBinaryTree(root)); } }
Attempts:
2 left
💡 Hint
Think about how the width is calculated including null positions between nodes.
✗ Incorrect
The maximum width is calculated as the maximum distance between the leftmost and rightmost nodes at any level, counting nulls in between. Here, the bottom level has nodes at positions 0 (5), 1 (3), and 3 (9), so width is 3 - 0 + 1 = 4.
❓ Predict Output
intermediateOutput for a skewed binary tree
What is the maximum width of the following skewed binary tree after running the code?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { public int widthOfBinaryTree(TreeNode root) { if (root == null) return 0; java.util.Queue<java.util.Map.Entry<TreeNode, Integer>> queue = new java.util.LinkedList<>(); queue.offer(new java.util.AbstractMap.SimpleEntry<>(root, 0)); int maxWidth = 0; while (!queue.isEmpty()) { int size = queue.size(); int minIndex = queue.peek().getValue(); int first = 0, last = 0; for (int i = 0; i < size; i++) { java.util.Map.Entry<TreeNode, Integer> entry = queue.poll(); TreeNode node = entry.getKey(); int index = entry.getValue() - minIndex; if (i == 0) first = index; if (i == size - 1) last = index; if (node.left != null) queue.offer(new java.util.AbstractMap.SimpleEntry<>(node.left, 2 * index + 1)); if (node.right != null) queue.offer(new java.util.AbstractMap.SimpleEntry<>(node.right, 2 * index + 2)); } maxWidth = Math.max(maxWidth, last - first + 1); } return maxWidth; } public static void main(String[] args) { TreeNode root = new TreeNode(1); root.right = new TreeNode(2); root.right.right = new TreeNode(3); root.right.right.right = new TreeNode(4); Solution sol = new Solution(); System.out.println(sol.widthOfBinaryTree(root)); } }
Attempts:
2 left
💡 Hint
In a skewed tree, width is usually 1 because nodes are aligned.
✗ Incorrect
Each level has only one node, so the width at every level is 1. The maximum width is therefore 1.
🔧 Debug
advancedIdentify the error in the width calculation code
What error will the following code produce when run on a binary tree with many levels?
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { public int widthOfBinaryTree(TreeNode root) { if (root == null) return 0; java.util.Queue<java.util.Map.Entry<TreeNode, Integer>> queue = new java.util.LinkedList<>(); queue.offer(new java.util.AbstractMap.SimpleEntry<>(root, 0)); int maxWidth = 0; while (!queue.isEmpty()) { int size = queue.size(); int minIndex = queue.peek().getValue(); int first = 0, last = 0; for (int i = 0; i < size; i++) { java.util.Map.Entry<TreeNode, Integer> entry = queue.poll(); TreeNode node = entry.getKey(); int index = entry.getValue() - minIndex; if (i == 0) first = index; if (i == size - 1) last = index; if (node.left != null) queue.offer(new java.util.AbstractMap.SimpleEntry<>(node.left, 2 * index + 1)); if (node.right != null) queue.offer(new java.util.AbstractMap.SimpleEntry<>(node.right, 2 * index + 2)); } maxWidth = Math.max(maxWidth, last - first + 1); } return maxWidth; } }
Attempts:
2 left
💡 Hint
Consider what happens to the index values at very deep levels.
✗ Incorrect
The index values double at each level, so for very deep trees, the integer index can overflow causing incorrect calculations.
🧠 Conceptual
advancedUnderstanding the indexing scheme in maximum width calculation
Why does the algorithm assign indices to nodes as 2 * index + 1 for left child and 2 * index + 2 for right child?
Attempts:
2 left
💡 Hint
Think about how the width is measured including gaps between nodes.
✗ Incorrect
Assigning indices this way simulates the positions of nodes in a complete binary tree, allowing calculation of width including gaps caused by missing nodes.
❓ Predict Output
expertMaximum width for a complex tree with missing nodes
What is the maximum width printed by the code for the following tree structure?
Tree:
- Root (1)
- Left (2)
- Right (5)
- Right (3)
- Right (4)
- Left (6)
- Right (7)
DSA Java
class TreeNode { int val; TreeNode left, right; TreeNode(int x) { val = x; } } public class Solution { public int widthOfBinaryTree(TreeNode root) { if (root == null) return 0; java.util.Queue<java.util.Map.Entry<TreeNode, Integer>> queue = new java.util.LinkedList<>(); queue.offer(new java.util.AbstractMap.SimpleEntry<>(root, 0)); int maxWidth = 0; while (!queue.isEmpty()) { int size = queue.size(); int minIndex = queue.peek().getValue(); int first = 0, last = 0; for (int i = 0; i < size; i++) { java.util.Map.Entry<TreeNode, Integer> entry = queue.poll(); TreeNode node = entry.getKey(); int index = entry.getValue() - minIndex; if (i == 0) first = index; if (i == size - 1) last = index; if (node.left != null) queue.offer(new java.util.AbstractMap.SimpleEntry<>(node.left, 2 * index + 1)); if (node.right != null) queue.offer(new java.util.AbstractMap.SimpleEntry<>(node.right, 2 * index + 2)); } maxWidth = Math.max(maxWidth, last - first + 1); } return maxWidth; } public static void main(String[] args) { TreeNode root = new TreeNode(1); root.left = new TreeNode(2); root.right = new TreeNode(3); root.left.right = new TreeNode(5); root.right.right = new TreeNode(4); root.right.right.left = new TreeNode(6); root.right.right.right = new TreeNode(7); Solution sol = new Solution(); System.out.println(sol.widthOfBinaryTree(root)); } }
Attempts:
2 left
💡 Hint
Calculate indices level by level and find the widest gap including nulls.
✗ Incorrect
The widest level is the last one with nodes at indices 9 (6) and 10 (7) and also nodes at earlier indices from previous levels, resulting in width 7.
