323. Sum of All Values
Print the total of every value in the tree.
Same traversal again, combining with addition. The empty subtree contributes 0, which is the natural identity for a sum and is why this base case feels obvious where the maximum's does not.
Watch the total. With 100000 nodes each near a billion, it reaches 10^14 and needs 64 bits.
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 all values in the tree.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.