Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Flatten Binary Tree to Linked List
Bookmark
Preorder + List
Reverse Postorder
Morris
Input
Example Tree
Simple Tree
Right-Skewed
Left-Skewed
Custom
tree
=
[1, 2, 5, 3, 4, null, 6]
1
2
5
3
4
6
list
collect nodes in preorder, then rewire
1
2
5
3
4
6
list
collect nodes in preorder, then rewire
1
2
5
3
4
6
#1
list
1
preorder visit
1
→ list #1
1
2
5
3
4
6
#1
#2
#3
list
1
→
2
→
3
preorder visit
3
→ list #3
1
2
5
3
4
6
#1
#2
#3
#4
list
1
→
2
→
3
→
4
preorder visit
4
→ list #4
1
2
5
3
4
6
#1
#2
#5
#3
#4
list
1
→
2
→
3
→
4
→
5
preorder visit
5
→ list #5
1
2
5
3
4
6
#1
#2
#5
#3
#4
#6
list
1
→
2
→
3
→
4
→
5
→
6
1
.right =
2
, .left = null
1
2
5
3
4
6
#1
#2
#5
#3
#4
#6
list
1
→
2
→
3
→
4
→
5
→
6
2
.right =
3
, .left = null
1
2
5
3
4
6
#1
#2
#5
#3
#4
#6
list
1
→
2
→
3
→
4
→
5
→
6
3
.right =
4
, .left = null
1
2
5
3
4
6
#1
#2
#5
#3
#4
#6
list
1
→
2
→
3
→
4
→
5
→
6
5
.right =
6
, .left = null
1
2
5
3
4
6
#1
#2
#5
#3
#4
#6
list
1
→
2
→
3
→
4
→
5
→
6
flattened: 1 → 2 → 3 → 4 → 5 → 6
algo
master
.
io
Step:
Start: collect the nodes in preorder, then rewire into a right chain
0 / 12
Input
Example Tree
Simple Tree
Right-Skewed
Left-Skewed
Custom
tree
=
[1, 2, 5, 3, 4, null, 6]
0 / 12
algo
master
.
io
Step:
Start: collect the nodes in preorder, then rewire into a right chain