Sum Root-to-Leaf Binary Numbers
Reported by candidates from Oracle's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Oracle OA reported in October 2026 hands you a binary tree of 0s and 1s and asks for the sum of every root-to-leaf binary number. It looks like a warm-up, and it mostly is. The trap is the leaf check. If you treat a node with one child as a leaf, you count partial paths and your answer comes out wrong on skewed trees. The pattern is tree traversal with a running value carried down. If you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the solution in real time. Here's the script.
The problem
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 its most significant bit. Return the sum of all represented numbers. A leaf is a node with no children. Function sumRootToLeaf(root: TreeNode) → int Examples Example 1 root = [1,0,1,0,1,0,1] return = 22 The four paths represent 100, 101, 110, and 111, which sum to 22. Example 2 root = [0] return = 0 The only path represents zero. Example 3 root = [1] return = 1 The only path represents one. Constraints The tree contains between 1 and 1000 nodes. node.val is 0 or 1. The sum fits a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to carry the number as you descend. At each node, compute cur = cur * 2 + node.val. When you hit a leaf, meaning both left and right are null, return cur. Otherwise return the sum of the left and right recursive calls. That's DFS, O(n) time, O(h) space. The common pitfall is the leaf condition. If you only check that the node is null and return cur there, a node with one child gets counted twice. Check for a true leaf instead. Another slip is building strings and parsing them at the end, which works but wastes effort. Example 2 and 3 are single-node trees, so make sure the root itself counts as a leaf. The sum fits in 32 bits, so you don't need big integers. If the recursion or the leaf logic slips under pressure, StealthCoder is the hedge on the live OA: it reads the problem and hands you the clean version.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Sum Root-to-Leaf Binary Numbers cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Oracle's OA.
Oracle reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Sum Root-to-Leaf Binary Numbers FAQ
How hard is Sum Root-to-Leaf Binary Numbers really?+
It's easy. One DFS with a running value, about ten lines. Most failures come from a wrong leaf check, not from the algorithm. If you can write a basic recursive tree traversal, you can finish this in a few minutes.
What's the trick to getting it right?+
Carry the value down: cur = cur * 2 + node.val. Return cur only at a true leaf, where both children are null. Otherwise sum the results from the left and right children. That covers every example, including the single-node ones.
What edge case breaks the naive solution?+
A node with exactly one child. If you return cur whenever a node is null, that path gets counted twice, once for the null side and once for the real side. Check for a leaf explicitly before returning the value.
Should I use recursion or iteration?+
Recursion is shorter and fine here. With up to 1000 nodes, depth can reach 1000 on a skewed tree, which is safe in most languages. If you prefer iterative, use a stack of node and value pairs and add to the total at leaves.
How do I prepare for this in 48 hours?+
Write the recursive solution from memory twice. Then test it by hand on a skewed tree, a single node, and the full example giving 22. Also review other tree DFS problems that pass state downward, since Oracle OAs often reuse that shape.