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 maximum width of the following binary tree?
Tree structure:
1
/ \
2 3
/ \
4 5
Assume the width is the maximum number of nodes at any level.
Tree structure:
1
/ \
2 3
/ \
4 5
Assume the width is the maximum number of nodes at any level.
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def width_of_binary_tree(root): if not root: return 0 max_width = 0 queue = [(root, 0)] # (node, index) while queue: level_length = len(queue) _, first_index = queue[0] for i in range(level_length): node, index = queue.pop(0) if node.left: queue.append((node.left, 2 * index)) if node.right: queue.append((node.right, 2 * index + 1)) max_width = max(max_width, index - first_index + 1) return max_width root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.right.right = TreeNode(5) print(width_of_binary_tree(root))
Attempts:
2 left
💡 Hint
Count the nodes at each level and consider the positions of nodes to calculate width.
✗ Incorrect
The maximum width is the largest number of nodes at any level. Here, the second level has nodes 2 and 3 (2 nodes), the third level has nodes 4 and 5 but they are at positions 0 and 3 respectively, so width is 4 (3 - 0 + 1). However, the code calculates width based on indices, so the maximum width is 4.
❓ Predict Output
intermediateOutput of Maximum Width for Skewed Tree
What is the maximum width of this skewed binary tree?
Tree structure:
1
\
2
\
3
\
4
Tree structure:
1
\
2
\
3
\
4
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def width_of_binary_tree(root): if not root: return 0 max_width = 0 queue = [(root, 0)] while queue: level_length = len(queue) _, first_index = queue[0] for i in range(level_length): node, index = queue.pop(0) if node.left: queue.append((node.left, 2 * index)) if node.right: queue.append((node.right, 2 * index + 1)) max_width = max(max_width, index - first_index + 1) return max_width root = TreeNode(1) root.right = TreeNode(2) root.right.right = TreeNode(3) root.right.right.right = TreeNode(4) print(width_of_binary_tree(root))
Attempts:
2 left
💡 Hint
In a skewed tree, only one node exists at each level.
✗ Incorrect
Each level has only one node, so the maximum width is 1.
🔧 Debug
advancedIdentify the Error in Width Calculation Code
What error will this code produce when calculating the maximum width of a binary tree?
DSA Python
def width_of_binary_tree(root): if not root: return 0 max_width = 0 queue = [(root, 0)] while queue: level_length = len(queue) _, first_index = queue[0] for i in range(level_length): node, index = queue.pop(0) if node.left: queue.append((node.left, index * 2 + 1)) if node.right: queue.append((node.right, index * 2 + 2)) max_width = max(max_width, index - first_index + 1) return max_width
Attempts:
2 left
💡 Hint
Check how child indices are assigned compared to the standard formula.
✗ Incorrect
The child indices should be 2 * index for left and 2 * index + 1 for right. Here, they are shifted by +1 and +2, causing incorrect width calculation.
🧠 Conceptual
advancedUnderstanding Width Calculation in Sparse Binary Trees
Why do we use indices (like 2 * index and 2 * index + 1) when calculating the maximum width of a binary tree?
Attempts:
2 left
💡 Hint
Think about how missing nodes affect width measurement.
✗ Incorrect
Using indices simulates positions in a complete binary tree, so gaps between nodes are counted, giving the true width including empty spots.
❓ Predict Output
expertMaximum Width of Complex Binary Tree
What is the maximum width of this binary tree?
Tree structure:
1
/ \
2 3
/ \
4 5
\ / \
6 7 8
\
9
Tree structure:
1
/ \
2 3
/ \
4 5
\ / \
6 7 8
\
9
DSA Python
class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def width_of_binary_tree(root): if not root: return 0 max_width = 0 queue = [(root, 0)] while queue: level_length = len(queue) _, first_index = queue[0] for i in range(level_length): node, index = queue.pop(0) if node.left: queue.append((node.left, 2 * index)) if node.right: queue.append((node.right, 2 * index + 1)) max_width = max(max_width, index - first_index + 1) return max_width root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(3) root.left.left = TreeNode(4) root.left.left.right = TreeNode(6) root.right.right = TreeNode(5) root.right.right.left = TreeNode(7) root.right.right.right = TreeNode(8) root.right.right.left.right = TreeNode(9) print(width_of_binary_tree(root))
Attempts:
2 left
💡 Hint
Track the indices of nodes at the widest level carefully.
✗ Incorrect
The widest level is the last one with nodes 6, 7, 8, and 9 positioned with indices that span 8 positions including gaps.
