Flatten Tree into a Right Chain
You are given the root of a binary tree. Rebuild the tree as a single chain in preorder: the new root is the old root, each node's left is empty, and each node's right is the next node in the preorder of the original tree. Return the root of the chain. An empty tree gives an empty tree.
Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.
Example 1
root [1,2,5,3,4,null,6]
returns [1,null,2,null,3,null,4,null,5,null,6]
Preorder is 1,2,3,4,5,6, so the chain is [1,None,2,None,3,None,4,None,5,None,6].
Constraints
0 <= number of nodes <= 100-100 <= node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.