Skip to content
Onsite

Flatten Binary Tree to Linked List

MediumP2Coding

Flatten a binary tree into a "linked list" in place. Afterwards every node's left must be null and its right must point to the next node, in the order of a preorder traversal of the original tree. Modify the tree you are given; the function returns nothing.

Example 1

Input
root = [1,2,5,3,4,null,6]
Output
[1,null,2,null,3,null,4,null,5,null,6]
Why
Preorder is 1, 2, 3, 4, 5, 6.

Example 2

Input
root = []
Output
[]

Example 3

Input
root = [0]
Output
[0]

Constraints

  • 0 <= number of nodes <= 2000
  • -100 <= Node.val <= 100

Hints

Run checks the 3 visible tests, Submit all 9.