Depth first search · Easy
A binary decision-tree inspector attaches an integer adjustment to each node. It checks whether any complete root-to-leaf choice sequence has a requested sum. Stopping early at an internal node is not a complete choice.
Return whether at least one reachable root-to-leaf path has node values summing exactly to target.
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)[], "target": number }.
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.
- target is an integer in [-100000000000000,100000000000000].
Output
Return value
- Return a boolean. A qualifying path must start at the root and end at a node with no reachable children.
- An empty tree returns false even for target=0.
- Negative values are allowed, so a partial sum exceeding target is not enough to discard a branch.
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
{
"target": 8,
"tree": [
5,
3,
8
]
}Returns
true5→3 is a complete path.
Example 2
{
"target": 5,
"tree": [
5,
3,
8
]
}Returns
falseThe root is not a leaf.
Required complexity
O(n) time and O(h) traversal space