Sum All Values in a Binary Tree
Problem statement
Given the root of a binary tree, return the sum of all node values. An empty tree has sum zero.
Function
sumTree(root: TreeNode) → longExamples
Example 1
root = [1,2,3]return = 6The node values sum to 1 + 2 + 3 = 6.
Example 2
root = []return = 0The empty tree has sum zero.
Example 3
root = [-5,2,8]return = 5Negative and positive node values are both included.
Constraints
- The tree contains between
0and100000nodes. - Node values are signed 32-bit integers.
- The total fits signed 64-bit arithmetic.