FastPrepSum All Values in a Binary Tree

Sum All Values in a Binary Tree

Duolingo logoDuolingo● EasyINTERNPHONE SCREEN
Learn

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) → long

Examples

Example 1

root = [1,2,3]return = 6

The node values sum to 1 + 2 + 3 = 6.

Example 2

root = []return = 0

The empty tree has sum zero.

Example 3

root = [-5,2,8]return = 5

Negative and positive node values are both included.

Constraints

  • The tree contains between 0 and 100000 nodes.
  • Node values are signed 32-bit integers.
  • The total fits signed 64-bit arithmetic.

More Duolingo problems

See Duolingo hiring insights
public long sumTree(TreeNode root) {
    // Write your code here.
}
root[1,2,3]
expected6
Checking account…