Given the root of a binary tree, flatten the tree in-place into a linked list (using right pointers, in preorder traversal order).
Process each node: find the rightmost node of the left subtree (the preorder predecessor of the right subtree), attach right subtree there, then move left subtree to the right.
- While root != null:
- If root.left != null: find rightmost of left subtree.
- Attach root.right to that rightmost node's right.
- Move root.left to root.right, set root.left = null.
- Advance root = root.right.
- Time Complexity: O(N)
- Space Complexity: O(1)