Sum All Values in a Binary Tree
Updated
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.
More Duolingo Phone Screen questions
- Group Timestamps into Consecutive-Day IntervalsPHONE SCREEN · Seen Nov 2026
- Return Every Matching DecryptionPHONE SCREEN · Seen Nov 2026
- Visit Desired Attractions Without Reusing a TrailPHONE SCREEN · Seen Oct 2026
- Correct an In-Place Array ReversalPHONE SCREEN · Seen Oct 2026
- Count Starts Reaching a Grid MaximumPHONE SCREEN · Seen Oct 2026
- Select the Top User MatchesPHONE SCREEN · Seen Oct 2026
- User Resource Transition ProbabilitiesPHONE SCREEN · Seen Oct 2026
- Encrypt and Decrypt StringsPHONE SCREEN · Seen Apr 2026
- Validate a Mahjong Hand PartitionPHONE SCREEN · Seen Nov 2025
- Directional Tiles: Find a Valid Reconfiguration PathPHONE SCREEN · Seen Oct 2022
Similar questions at other companies
- Merge Two Binary TreesSnowflake · PHONE SCREENSnowflake interview questions
- Sum Root-to-Leaf Binary NumbersOracle · PHONE SCREENOracle interview questions
- Symmetric Binary TreeDocuSign · PHONE SCREENDocuSign interview questions
- Count One-Valued Regions in a Binary TreeGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Maximum Value in a Binary TreeAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Root-to-Leaf Paths as StringsAdobe · PHONE SCREENAdobe interview questions
- Left View of a Binary TreeMygate · PHONE SCREENMygate interview questions
- Binary Tree Preorder TraversalMorgan Stanley · PHONE SCREENMorgan Stanley interview questions
- Range Sum of BSTMeta · PHONE SCREENMeta interview questions