#114

Flatten Binary Tree to Linked List

pupil · 555 · lc medium +28 · verified · 70.3% accepted · 13,625 likes · top 79%

play →

Description

Rearrange the binary tree rooted at root into an in-place linked list:

- Reuse the same TreeNode class: the right pointer links to the next node and the left pointer must always be null.

- The resulting order must follow a pre-order traversal of the original tree.

Example 1:

Input: root = [1,2,5,3,4,null,6]
Output: [1,null,2,null,3,null,4,null,5,null,6]

Example 2:

Input: root = []
Output: []

Example 3:

Input: root = [0]
Output: [0]

Code

1
2
3
4
5
6
7
8
9
10
11
12