662. Maximum Width of Binary Tree - Explanation
Prerequisites
Before attempting this problem, you should be comfortable with:
- Binary Tree Basics - Understanding tree structure, nodes, and parent-child relationships
- Breadth-First Search (BFS) - Level-order traversal using a queue data structure
- Depth-First Search (DFS) - Recursive tree traversal techniques
- Tree Node Indexing - Assigning positional indices to nodes (left child = 2i, right child = 2i+1)
1. Breadth First Search
Intuition
The width of a level is defined by the distance between the leftmost and rightmost non-null nodes, including any null nodes in between. To measure this, we assign each node a position number: the root is 1, and for any node at position p, its left child is at 2 * p and right child at 2 * p + 1. Using BFS, we traverse level by level and compute the width as the difference between the last and first position on each level, plus one.
Algorithm
- Initialize
res = 0and a queue with(root, 1, 0)representing(node, position, level). - Track
prevLevelandprevNumto record the first position on each level. - While the queue is not empty:
- Dequeue
(node, num, level). - If
level > prevLevel, updateprevLevelandprevNumto the current level and position. - Update
res = max(res, num - prevNum + 1). - Enqueue the left child with position
2 * numand the right child with2 * num + 1, both atlevel + 1.
- Dequeue
- Return
res.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
res = 0
q = deque([(root, 1, 0)]) # [node, num, level]
prevLevel, prevNum = 0, 1
while q:
node, num, level = q.popleft()
if level > prevLevel:
prevLevel = level
prevNum = num
res = max(res, num - prevNum + 1)
if node.left:
q.append((node.left, 2 * num, level + 1))
if node.right:
q.append((node.right, 2 * num + 1, level + 1))
return resTime & Space Complexity
- Time complexity:
- Space complexity:
2. Breadth First Search (Optimal)
Intuition
Position numbers can grow large in deep skewed trees, potentially causing overflow. To prevent this, we normalize positions at each level by subtracting the starting position of that level. This keeps the numbers small while still correctly computing the width as the difference between the rightmost and leftmost positions on each level.
Algorithm
- Initialize
res = 0and a queue with(root, 0). - While the queue is not empty:
- Record
startas the position of the first node in the current level. - For each node in the current level:
- Compute
curNum = num - startto normalize. - Update
res = max(res, curNum + 1). - Enqueue children with positions
2 * curNumand2 * curNum + 1.
- Compute
- Record
- Return
res.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
res = 0
q = deque([(root, 0)])
while q:
start = q[0][1]
for _ in range(len(q)):
node, num = q.popleft()
curNum = num - start
res = max(res, curNum + 1)
if node.left:
q.append((node.left, 2 * curNum))
if node.right:
q.append((node.right, 2 * curNum + 1))
return resTime & Space Complexity
- Time complexity:
- Space complexity:
3. Depth First Search
Intuition
We can also solve this with DFS by recording the first position encountered at each level. As we traverse, whenever we visit a node, we check if its level has been seen before. If not, we record its position as the first for that level. The width at any node is computed as the difference from the first position on its level. We normalize child positions to avoid overflow.
Algorithm
- Maintain a map
firstwherefirst[level]stores the position of the first node visited at that level. - Define
dfs(node, level, curNum):- If
nodeis null, return. - If
levelnot infirst, setfirst[level] = curNum. - Update
res = max(res, curNum - first[level] + 1). - Recurse on left child with
level + 1and position2 * (curNum - first[level]). - Recurse on right child with
level + 1and position2 * (curNum - first[level]) + 1.
- If
- Call
dfs(root, 0, 0)and returnres.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def widthOfBinaryTree(self, root: Optional[TreeNode]) -> int:
first = {}
res = 0
def dfs(node, level, num):
nonlocal res
if not node:
return
if level not in first:
first[level] = num
res = max(res, num - first[level] + 1)
dfs(node.left, level + 1, 2 * num)
dfs(node.right, level + 1, 2 * num + 1)
dfs(root, 0, 0)
return resTime & Space Complexity
- Time complexity:
- Space complexity:
Common Pitfalls
Integer Overflow from Position Numbers
Position numbers double at each level (2 * num for left child, 2 * num + 1 for right). In a deep, skewed tree, these values can overflow standard integer types. Use larger types like unsigned long long in C++ or BigInt in JavaScript, or normalize positions by subtracting the starting position at each level.
Miscounting the Width
Width is defined as the number of nodes between the leftmost and rightmost positions on a level, inclusive. The formula is rightmost - leftmost + 1, not just the difference. Forgetting the + 1 gives a width that is one less than correct.
Incorrect Level Tracking in BFS
When processing nodes level by level, you must correctly identify the first node of each new level to record its starting position. If you mix up the level boundaries or fail to update the starting position when entering a new level, the width calculation will be incorrect.