codes of three-digit numbers, one per node:
- the hundreds digit is the node's depth d (the root is at depth 1, 1 ≤ d ≤ 4);
- the tens digit is the node's position p within its row as if the tree were complete (1 ≤ p ≤ 2^(d-1), left to right);
- the units digit is the node's value v (0 ≤ v ≤ 9).
So the children of the node at position p sit one level deeper at positions 2p - 1 (left) and 2p (right).
For every leaf, add up the values on the path from the root to it, and return the grand total over all leaves. The input is guaranteed to describe a valid connected tree.codes=[117, 212, 224, 325] → 25
The root 7 has children 2 and 4; node 2 has a right child 5 (position 2 at depth 3). The leaves are 5 and 4: the paths give 7 + 2 + 5 = 14 and 7 + 4 = 11, 25 in total.
codes=[110] → 0
A single root holding 0: one path summing to 0.
codes=[113, 229, 343, 488] → 23
codes is sorted in ascending ordercodes describes a valid binary tree of depth at most 4codes=[117, 212, 224, 325]
25
Run checks the first 3 tests in your browser. Submit checks all 115 tests on the server, hidden ones included.