Recover a Tree from Preorder Depth Encoding
Reported by candidates from Superhuman's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Superhuman reportedly put this one in front of candidates in July 2026, and the detail that trips people is the hyphen count: every node's depth is just the dashes before its number. Rebuild a binary tree from a string like "1-2--3--4-5--6--7" and return the root. It's a tree problem wearing a parsing costume. The one-child rule (a lone child is always left) removes the ambiguity that makes this harder elsewhere. If you blank on the stack logic mid-assessment, StealthCoder runs invisibly on your desktop and hands you the solution while the proctor sees nothing.
The problem
A binary tree is serialized by a preorder traversal. Each node is written as its positive integer value, preceded by a number of hyphens equal to its depth. The root has depth 0. For this exercise, assume every node has at most two children. When a node has exactly one child, that child is its left child. The input is a valid encoding of exactly one tree. Given the serialized string traversal, reconstruct the tree and return its root. Function recoverFromPreorder(traversal: String) → TreeNode Examples Example 1 traversal = "1-2--3--4-5--6--7" return = [1,2,5,3,4,6,7] Depth 0 gives root 1. The depth-1 nodes are 2 and 5; each receives its following depth-2 nodes as children. Example 2 traversal = "1-2--3---4-5--6---7" return = [1,2,5,3,null,6,null,4,null,7] The depth increases from 2 to 3 before values 4 and 7, so they become the left children of 3 and 6. Example 3 traversal = "1-401--349---90--88" return = [1,401,null,349,88,90] After the depth-3 node 90, the encoding returns to depth 2, so 88 becomes the right child of 401. Constraints 1 <= number of nodes <= 1000. 1 <= node.val <= 10^9. traversal contains only decimal digits and hyphens. traversal is a valid preorder depth encoding under the stated one-child rule.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a stack that mirrors the current root-to-node path. Parse the string left to right: count hyphens to get depth, then read the digits to get the value. Create the node, then pop the stack until its size equals the depth. The top is now the parent. If the parent has no left child, attach it there. Otherwise attach it right. Push the new node. Return the bottom of the stack, which is the root. The classic pitfall is parsing values as single digits, since values go up to 10^9. Another is forgetting to pop before attaching, which hangs nodes on the wrong parent in Example 3 when depth drops from 3 back to 2. A recursive version with a shared index also works. If your mind goes blank live, StealthCoder is the hedge that gets you the stack version fast.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Recover a Tree from Preorder Depth Encoding 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Superhuman's OA.
Superhuman reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Recover a Tree from Preorder Depth Encoding FAQ
What's the trick to Recover a Tree from Preorder Depth Encoding?+
Keep a stack holding the current path from root to the latest node. For each token, read the dash count as depth, pop until the stack size equals that depth, then attach the new node to the top. Left slot first, right slot if left is taken. Push the new node and continue.
How hard is this problem really?+
It's a medium that feels harder than it is. The algorithm is about fifteen lines once you see the stack. Most of the difficulty is careful parsing: counting hyphens, then reading multi-digit numbers up to 10^9 without off-by-one errors.
Why does the one-child-goes-left rule matter?+
Without it, a single child could be left or right and the encoding couldn't tell you which. With it, the rule is simple: attach to left if empty, otherwise right. That's why Example 2 puts 4 and 7 as left children with null on the right.
Should I use recursion or a stack?+
Either works with up to 1000 nodes. The stack is easier to debug because depth is compared directly to stack size. Recursion needs a shared index and a expected-depth argument. Pick whichever you can write without hesitation under pressure.
How do I prepare for this in 48 hours?+
Write the parser first and test it on Example 3, "1-401--349---90--88", since it has multi-digit values and a depth drop. Then add the stack logic. Trace by hand how the stack pops when depth returns from 3 to 2. Do that once and the pattern sticks.