You get the root of a binary tree. Flatten it into a chain, reusing the same nodes:
right pointer leads to the next node of the chain, and the last node's right is null;left pointer is null.Change the tree in place and return nothing: the answer is the tree read from root once your function finishes. It is printed like any tree, level by level with null for a missing child, so a flat chain prints as [a,null,b,null,c,…].
The whole left subtree of 5 (3, 2, 4) comes straight after it, ahead of its right subtree (8, 9).
6 was the left child of 1, so it moves to be its right child.
An empty tree stays empty.
Constraints
0 and 2000 nodes.-100 <= Node.val <= 100In preorder, a node's whole left subtree comes straight after it, and its right subtree comes after the last node of that left subtree. So the right subtree belongs hanging off the rightmost node of the left subtree.