Depth first search · Easy
A hierarchy inspector reads a serialized binary tree and reports its maximum nesting depth. Missing nodes can leave unreachable values later in the array. Array length alone therefore does not reveal the true depth.
Return the number of reachable nodes on the longest path from the root to a leaf.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
A JSON object { "tree": (number | null)[] }.
Fields
- tree is a heap-indexed array: children of index i are 2i+1 and 2i+2. Null or an out-of-range index has no node. Entries below a missing ancestor are unreachable and ignored.
Output
Return value
- Return an integer node count, not an edge count.
- An absent root has depth 0. A root with no children has depth 1.
- Ignore all entries whose ancestor is missing.
Constraints
- 0 <= tree.length <= 100000. Node values are integers in [-1000000000,1000000000].
- This is not a compact breadth-first serialization: missing child slots keep their indexes.
Examples
Example 1
{
"tree": [
1,
2,
3,
4
]
}Returns
3The path 1→2→4 has three nodes.
Example 2
{
"tree": [
null,
1,
2
]
}Returns
0There is no root.
Required complexity
O(n) time and O(h) traversal space