root of a binary tree, flatten the tree into a "linked list" in-place.
The "linked list" should use TreeNode right pointers only, be in preorder traversal order, and all left pointers should be null.
The simple way is to store the preorder in a list, and then relink the nodes one by one. That uses O(n) extra space. We can rewire the pointers directly, with O(1) extra space.
Walk down the tree with a pointer called current. If current has a left subtree, find the rightmost node of that left subtree. That rightmost node is the last one in preorder before the right subtree, so attach the right subtree to it. Then move the left subtree to the right side, and set left to null. Move current to its right child, and repeat until current is null.
The rightmost node of the left subtree comes right before the right subtree. Splice there, move left to right, and keep walking right.
Run the animation above on your own input, or watch the 2:26 video explanation of Flatten Binary Tree to Linked List with a dry run and Java code.