Sum Root-to-Leaf Node Values
Updated
Problem statement
You are given the root of a nonempty binary tree whose nodes contain digit values. For each root-to-leaf path, add the values of its nodes. Return the sum of these path sums across every root-to-leaf path.
Each path contributes its node values directly. Do not interpret the path as a multi-digit decimal number. A leaf is a node with no children.
Function
sumRootToLeafValues(root: TreeNode) → intExamples
Example 1
root = [1,2,3]return = 7The two root-to-leaf path sums are 1 + 2 = 3 and 1 + 3 = 4, for a total of 7.
Example 2
root = [4,9,0,5,1]return = 36The three path sums are 4 + 9 + 5 = 18, 4 + 9 + 1 = 14, and 4 + 0 = 4. Their total is 36.
Example 3
root = [9]return = 9The root is also the only leaf, so its single path contributes 9.
Constraints
- The tree has between
1and1000nodes. - Each node value is an integer from
0through9. - The tree depth is at most
10.
More Meta Onsite Interview questions
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Diagonal Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Render an Org Chart and Find Skip-Level PairsSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Find Leaves of a Binary TreeLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
- JSON Path Lookup with Implicit Array WildcardsMongoDB · ONSITE INTERVIEWMongoDB interview questions
- Delete and Compact a Forest SubtreePinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions
- Onion-Peel Layers of a Binary TreeeBay · ONSITE INTERVIEWeBay interview questions
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences