Find the widest level of a binary tree, counting the null slots between its end nodes

Quick Overview

Return the largest width over all levels of a binary tree, where a level's width runs from its leftmost to its rightmost node and counts the empty complete-tree slots in between. Tests level-order traversal with position indices and overflow-safe indexing.

Find the widest level of a binary tree, counting the null slots between its end nodes

Company: Dropbox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the `root` of a binary tree, return the maximum width of the tree. The maximum width of a tree is the largest width among all of its levels. The width of one level is the length between its end nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end nodes that would be present in a complete binary tree are also counted. It is guaranteed that the answer fits in a 32-bit signed integer. ### Function Signature ```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: Optional[TreeNode]) -> int: ``` ### Rules - The examples write the tree as a level-order list with `None` for a missing child. Children of a missing node are not listed, and trailing `None` values are dropped. - Picture each level as it would look in a complete binary tree of the same depth, with every slot present. A level's width counts every slot from its leftmost non-null node to its rightmost non-null node, inclusive, whether or not those slots hold a node. Slots outside the two end nodes do not count. - A level with a single node has width 1. ### Constraints - The number of nodes in the tree is in the range `[1, 3000]`. - `-100 <= Node.val <= 100` ### Examples ```text Input: root = [1,3,2,5,3,None,9] Output: 4 ``` The third level holds 5, 3, an empty slot, and 9, so its width is 4. The first two levels have widths 1 and 2. ```text Input: root = [1,3,2,5,None,None,9,6,None,None,7] Output: 8 ``` Here 5 has only a left child, 6, and 9 has only a right child, 7. On the fourth level 6 sits in the first slot and 7 in the eighth, with six empty slots between them, so the width is 8. ```text Input: root = [1,3,2,5] Output: 2 ``` The second level holds 3 and 2 side by side, for a width of 2. The third level holds only 5, so the widest level is not the deepest one.

Overview: Return the largest width over all levels of a binary tree, where a level's width runs from its leftmost to its rightmost node and counts the empty complete-tree slots in between. Tests level-order traversal with position indices and overflow-safe indexing.

Dropbox logo
Dropbox
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the root of a binary tree, return the maximum width of the tree.

The maximum width of a tree is the largest width among all of its levels. The width of one level is the length between its end nodes (the leftmost and rightmost non-null nodes), where the null nodes between the end nodes that would be present in a complete binary tree are also counted.

It is guaranteed that the answer fits in a 32-bit signed integer.

Function Signature

# 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: Optional[TreeNode]) -> int:

Rules

  • The examples write the tree as a level-order list with None for a missing child. Children of a missing node are not listed, and trailing None values are dropped.
  • Picture each level as it would look in a complete binary tree of the same depth, with every slot present. A level's width counts every slot from its leftmost non-null node to its rightmost non-null node, inclusive, whether or not those slots hold a node. Slots outside the two end nodes do not count.
  • A level with a single node has width 1.

Constraints

  • The number of nodes in the tree is in the range [1, 3000] .
  • -100 <= Node.val <= 100

Examples

Input: root = [1,3,2,5,3,None,9]
Output: 4

The third level holds 5, 3, an empty slot, and 9, so its width is 4. The first two levels have widths 1 and 2.

Input: root = [1,3,2,5,None,None,9,6,None,None,7]
Output: 8

Here 5 has only a left child, 6, and 9 has only a right child, 7. On the fourth level 6 sits in the first slot and 7 in the eighth, with six empty slots between them, so the width is 8.

Input: root = [1,3,2,5]
Output: 2

The second level holds 3 and 2 side by side, for a width of 2. The third level holds only 5, so the widest level is not the deepest one.