Flatten Binary Tree to Linked List
Given the root of a binary tree, flatten it in place into a linked list. The linked list uses the same TreeNode class where each node's right child points to the next node in preorder and each node's left child is set to null.
Open official problem prompt ↗Rearrange the tree in place so following right pointers visits nodes in preorder and no left pointers remain.
Think of each left branch as a detour that must be spliced into the main road: you connect the end of the detour back to where the main road continued, then reroute the main road through the detour.
- Input
- root = [1,2,5,3,4,null,6]
- Output
- [1,null,2,null,3,null,4,null,5,null,6]
- Why
- Preorder is 1,2,3,4,5,6; each becomes the right child of the previous with all left children nulled.
The number of nodes is in the range [0, 2000]-100 <= Node.val <= 100