Flatten Binary Tree to Linked List
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Every left child gets set to null and the whole tree collapses into a right-only chain in preorder. That's the Bloomberg OA question reported in June 2021, and the example is the classic 1,2,5,3,4,null,6 tree turning into 1,2,3,4,5,6. It's a tree problem with a clean in-place trick, and it's easy to overthink under a timer. If you blank on the pointer rewiring during the live assessment, StealthCoder runs invisibly on your desktop and gives you the solution as a safety net. Know the shape of the answer first, though. You'll write it faster and trust it more.
The problem
Flatten the binary tree rooted at root in place into a right-child-only chain whose node order is the tree's preorder traversal. Set every left child to null and return root. Function flattenTree(root: TreeNode) → TreeNode Examples Example 1 root = [1,2,5,3,4,null,6] return = [1,null,2,null,3,null,4,null,5,null,6] The right chain follows preorder 1,2,3,4,5,6. Constraints The tree contains at most 2000 nodes. Node values fit in signed 32-bit integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is rewiring pointers, not building a list. For each node with a left child, find the rightmost node of the left subtree. Attach the original right subtree to that node's right pointer. Then move the left subtree to the right and set left to null. Step to the next node and repeat. That's O(n) time and O(1) extra space, the Morris-style approach. The recursive version is also fine: flatten right, flatten left, and keep a prev pointer, building the chain in reverse preorder. The common pitfall is overwriting node.right before saving it, which orphans the whole right subtree. Another is forgetting to null the left child, so the output has stray left links. With up to 2000 nodes, recursion depth is safe. If the rewiring order slips your mind mid-OA, StealthCoder is the hedge that keeps you moving.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Flatten Binary Tree to Linked List 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 passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as flatten binary tree to linked list. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Flatten Binary Tree to Linked List FAQ
How hard is Flatten Binary Tree to Linked List really?+
Medium. The idea is short, but the pointer order trips people up. Once you see that the left subtree's rightmost node should link to the old right child, it's about ten lines. Most failures come from losing a reference, not from the algorithm.
What's the trick for the in-place requirement?+
Find the rightmost node of the left subtree, hang the original right subtree off it, then move the left subtree to the right and null the left. Repeat down the chain. No extra list, no recursion needed, constant extra space.
Can I just do a preorder traversal and rebuild the tree?+
You can, and it works with 2000 nodes. Store nodes in a list, then relink each one's right to the next and left to null. It uses O(n) space, though. If the problem says in place, the interviewer may expect the O(1) approach.
Is the tree pattern still asked in Bloomberg OAs?+
This one was reported in June 2021, and tree traversal and restructuring problems remain common across company assessments. Know preorder, postorder, and pointer manipulation cold. They show up in many variations.
How do I prepare for this in 48 hours?+
Write the iterative rightmost-node version from scratch twice, then the recursive reverse-preorder version once. Test on the 1,2,5,3,4,null,6 example and on a tree with only left children. Those two cases catch most bugs.