Flatten Binary Tree to Linked List
Problem statement
Flatten the binary tree rooted at root in place into a right-child-only chain whose node order is the tree's preorder traversal. Set every left child to null and return root.
Function
flattenTree(root: TreeNode) → TreeNodeExamples
Example 1
root = [1,2,5,3,4,null,6]return = [1,null,2,null,3,null,4,null,5,null,6]The right chain follows preorder 1,2,3,4,5,6.
Constraints
- The tree contains at most 2000 nodes.
- Node values fit in signed 32-bit integers.