338. Sum of the Leaves
Print the total of the values held by leaves only. Internal nodes contribute nothing.
This is problem 325 adding values instead of counting, and it has the same trap: child 0 is not a leaf. A leaf is a real node whose two children are both 0.
Totals need 64 bits, and negative values are allowed, so a total initialised to anything other than 0 is wrong.
Constraints - `1 ≤ n ≤ 100000` - `-1000000000 ≤ value ≤ 1000000000` - The input always forms a valid tree rooted at node 1.
Input
The first line contains an integer n, the number of nodes.
The second line contains n values, where node i holds the ith value.
Each of the next n lines contains two integers, the left and right child of
node i, using 0 for no child. Node 1 is the root.
Output
Print one integer, the sum of the values at the leaves.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.