Tree traversal · Easy
A tree viewer allocates one row of controls for each depth. It needs the largest number of actual nodes on a single row. Missing heap slots are not controls, so counting the span between the leftmost and rightmost index would allocate unnecessary capacity.
Return the maximum number of reachable nodes at any one depth counting actual nodes rather than index gaps.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ tree: (integer | null)[] }
Fields
- tree is a binary tree encoded with heap indexes: root 0; children of i are 2i+1 and 2i+2. This is not compact queue serialization.
- Null parents make their descendants unreachable. Values do not affect this structural measurement.
Output
Return value
- Return an integer count. Empty input or a null root returns zero.
- Null holes and unreachable descendants never contribute. A singleton tree has width one.
Constraints
- 0 <= tree.length <= 100000; each non-null value is an integer from -1000000000 to 1000000000.
- Do not mutate either input array. Sums fit safe JSON integers. Heap indexing bounds the tree height by 17 at this input limit.
Examples
Example 1
{
"tree": [
1,
2,
3,
4,
null,
null,
7
]
}Returns
2Example 2
{
"tree": [
1
]
}Returns
1Required complexity
O(n) time and O(h) auxiliary traversal space excluding the returned output