Sum of all values
Standard · Write code · about 6 min
Given the root of a binary tree, return the sum of all node values. An empty tree has sum 0.
Example 1
Input:
[[8,3,10,1,6,null,14]]
Output:
42
8 + 3 + 10 + 1 + 6 + 14 = 42.
Example 2
Input:
[[1,null,2,3]]
Output:
6
Constraints
- 0 ≤ number of nodes ≤ 1,000
- -1,000 ≤ Node.val ≤ 1,000
- The tree is given in level order; null marks a missing child
Learn the idea first: Binary trees and how they are stored, a visual lesson.
More Binary trees and how they are stored problems
- Build from a level-order array Warm-up
- Children of index i Warm-up
- Which tree is complete? Standard
- Count the nodes Standard
- Check completeness Challenge