Sum Root-to-Leaf Binary Numbers
Updated
Problem statement
You are given the root of a binary tree whose node values are 0 or 1.
Each root-to-leaf path represents a binary number, with the root as the leftmost, most significant bit. Return the sum of all represented numbers.
A leaf is a node with no children.
Function
sumRootToLeaf(root: TreeNode) → intExamples
Example 1
root = [1,0,1,0,1,0,1]return = 22The four paths represent 100, 101, 110, and 111, which sum to 22.
Example 2
root = [0]return = 0The only path represents zero.
Example 3
root = [1]return = 1The only path represents one.
Constraints
- The tree contains between
1and1000nodes. node.valis0or1.- The sum fits a signed 32-bit integer.
More Oracle Phone Screen questions
- Remove Parentheses from a Plus-Minus ExpressionPHONE SCREEN · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · Seen Oct 2026
- Shortest Window by Distinct-Value SumPHONE SCREEN · Seen Oct 2026
- Detect a Cycle in a Directed GraphPHONE SCREEN · Seen Oct 2026
- Time-Based Key-Value StorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Remove Adjacent Duplicates in String IIPHONE SCREEN · Seen Oct 2026
- Longest All-Present Attendance StreakPHONE SCREEN · Seen Oct 2026
- Next PermutationPHONE SCREEN · Seen Sep 2026
- Valid ParenthesesPHONE SCREEN · Seen Sep 2026
- Valid Parenthesis StringPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Merge Two Binary TreesSnowflake · PHONE SCREENSnowflake interview questions
- Sum All Values in a Binary TreeDuolingo · PHONE SCREENDuolingo 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