Problem
Section titled “Problem”Given the root of a binary tree, flatten the tree into a “linked list”:
- The “linked list” should use the same
TreeNodeclass where therightchild pointer points to the next node in the list and theleftchild pointer is alwaysnull. - The “linked list” should be in the same order as a pre-order traversal of the binary tree.
Example 1:

Input: root = [1,2,5,3,4,null,6]Output: [1,null,2,null,3,null,4,null,5,null,6]Example 2:
Input: root = []Output: []Example 3:
Input: root = [0]Output: [0]Constraints:
- The number of nodes in the tree is in the range
[0, 2000]. -
Node.val
Follow-up: Can you flatten the tree in-place (with O(1) extra space)?
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Pre-order visits a node, then its whole left subtree, then its right subtree. Inside the left subtree, the last node in pre-order is its rightmost node (keep going right until there is no right child). So for a node curr with a left child, the right subtree must come right after that rightmost node.
That gives a local rewiring that keeps pre-order intact: hang curr->right off the rightmost node of the left subtree, then move the left subtree into curr->right and clear curr->left. Now curr has no left child, and the pre-order of the tree is unchanged. Repeating this at every node while walking down the right pointers flattens the tree using only a couple of pointers, which answers the follow-up (the same idea as a Morris traversal). A recursive or stack-based DFS would need extra space instead.
Algorithm
Section titled “Algorithm”- Set
curr = root. - While
curris notnullptr:- If
curr->leftexists, find its rightmost nodepreby followingrightpointers fromcurr->left. - Set
pre->right = curr->right, thencurr->right = curr->leftandcurr->left = nullptr. - Move to
curr = curr->right.
- If
- Every node now has
left == nullptr, and therightpointers follow pre-order.
Walkthrough
Section titled “Walkthrough”Example 1. At curr = 1, the left subtree is rooted at 2 and its rightmost node is 4. The right subtree 5 → 6 is hung off 4, and the subtree at 2 moves to 1’s right (dashed edges are the new links):
Next curr = 2: its left child 3 has no right child, so pre = 3; 3->right = 4, 2->right = 3. The remaining nodes have no left children, and the list reads 1 → 2 → 3 → 4 → 5 → 6.
Complexity
Section titled “Complexity”- Time: —
currvisits each node once. A search forprewalks right-edges on the right spine ofcurr’s left subtree. After the splice, every node on that spine has all its ancestors reaching it through right-edges, so it is never again inside some node’s left subtree and its edge is never walked by a later search. Hence all searches together walk each edge at most once. - Space: — only
currandpre; no recursion and no new nodes.
Edge cases
Section titled “Edge cases”- Empty tree (Example 2):
currstarts asnullptr, so the loop does nothing. - Single node (Example 3) or an already flat tree with only right children: no node has a left child, so
currsimply walks the chain. - Only left children (a left-leaning path): at each node
preis the single left child, and the chain is turned to the right one node at a time.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: void flatten(TreeNode* root) { for (TreeNode* curr = root; curr; curr = curr->right) { if (!curr->left) continue; TreeNode* pre = curr->left; // last node of the left subtree in pre-order while (pre->right) pre = pre->right; pre->right = curr->right; curr->right = curr->left; curr->left = nullptr; } }};class Solution { public void flatten(TreeNode root) { for (TreeNode curr = root; curr != null; curr = curr.right) { if (curr.left == null) continue; TreeNode pre = curr.left; // last node of the left subtree in pre-order while (pre.right != null) pre = pre.right; pre.right = curr.right; curr.right = curr.left; curr.left = null; } }}class Solution: def flatten(self, root: TreeNode | None) -> None: """ Do not return anything, modify root in-place instead. """ curr = root while curr: if curr.left: pre = curr.left # last node of the left subtree in pre-order while pre.right: pre = pre.right pre.right = curr.right curr.right = curr.left curr.left = None curr = curr.rightGive it a try first
Attempt the problem on LeetCode — the code will still be here.
