Data Structures19 sections · 729 units
Open in Course

Problem - Flatten to Linked List

Preorder to list

Given the root of a binary tree, flatten the tree into a "linked list": The linked list should use the same TreeNode class.

The right pointer points to the next node, and left is always null. The list should be in preorder. Example: [1,2,5,3,4,null,6][1,2,5,3,4,null,6] becomes 1234561 \to 2 \to 3 \to 4 \to 5 \to 6.

Do it in-place using O(1)O(1) extra space (excluding recursion stack). Constraints: up to 20002000 nodes, values from 100-100 to 100100.