Measure Binary-Tree Width Including Internal Null Gaps

Quick Overview

Measure Binary-Tree Width Including Internal Null Gaps. Return the greatest width among levels of root.

Measure Binary-Tree Width Including Internal Null Gaps

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the greatest width among levels of root. A level's width counts complete-tree slots from its leftmost nonnull node through its rightmost nonnull node, including missing slots between them. The result fits signed 32-bit int. ```cpp class Solution { public: int widthOfBinaryTree(TreeNode* root); }; ``` ### Examples **Example 1:** ```text root = [8,4,12,2,null,null,14] Output: 4 Explanation: The last level spans slots [2,null,null,14]. ``` **Example 2:** ```text root = [5,null,7,null,9] Output: 1 Explanation: A single node at each level has width one. ``` ### Constraints - Tree size: 1..3000 nodes, with values in -100..100. The final width has the signed-32-bit fit promise stated above. ### Clarifying Questions Include null slots between endpoints and prevent index overflow without changing relative positions. ```hint Read the boundary condition Can subtracting the same level offset preserve every width calculation? ``` ### Contract considerations Include null slots between endpoints and prevent index overflow without changing relative positions.

Overview: Measure Binary-Tree Width Including Internal Null Gaps. Return the greatest width among levels of root.

Amazon logo
Amazon
Sep 10, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the greatest width among levels of root. A level's width counts complete-tree slots from its leftmost nonnull node through its rightmost nonnull node, including missing slots between them. The result fits signed 32-bit int.

class Solution {
public:
    int widthOfBinaryTree(TreeNode* root);
};

Examples

Example 1:

root = [8,4,12,2,null,null,14]
Output: 4
Explanation: The last level spans slots [2,null,null,14].

Example 2:

root = [5,null,7,null,9]
Output: 1
Explanation: A single node at each level has width one.

Constraints

  • Tree size: 1..3000 nodes, with values in -100..100. The final width has the signed-32-bit fit promise stated above.

Clarifying Questions Guidance

Include null slots between endpoints and prevent index overflow without changing relative positions.

Contract considerations

Include null slots between endpoints and prevent index overflow without changing relative positions.