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
flatten into a right-only preorder chain
1
2
5
3
4
6
list
flatten into a right-only preorder chain
1
2
5
3
4
6
list
predecessor =
2
(current.left)
1
2
5
3
4
6
list
1
.right =
2
(current.left)
1
2
5
3
4
6
#1
list
1
settled
#1
: current =
2
1
2
5
3
4
6
#1
list
1
3
.right =
4
(current.right)
1
2
5
3
4
6
#1
list
1
2
.left = null
1
2
5
3
4
6
#1
#2
#3
list
1
→
2
→
3
settled
#3
: current =
4
1
2
5
3
4
6
#1
#2
#3
#4
list
1
→
2
→
3
→
4
settled
#4
: current =
5
1
2
5
3
4
6
#1
#2
#5
#3
#4
list
1
→
2
→
3
→
4
→
5
current =
6
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: flatten the tree into a right-only chain in preorder order
0 / 22
Input
Example Tree
Simple Tree
Right-Skewed
Left-Skewed
Custom
tree
=
[1, 2, 5, 3, 4, null, 6]
0 / 22
algo
master
.
io
Step:
Start: flatten the tree into a right-only chain in preorder order