Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Binary Search Tree Iterator
Bookmark
Flatten Array
Stack (Optimal)
Morris O(1)
Input
Standard
Complete BST
Sparse BST
Custom
tree
=
[7, 3, 15, null, null, 9, 20]
7
3
15
9
20
constructor
values
flatten the BST into a sorted array up front
7
3
15
9
20
constructor
values
flatten the BST into a sorted array up front
7
3
#1
node
15
9
20
constructor
values
3
0
values.append(
3
)
7
#2
3
#1
15
9
#3
node
20
constructor
values
3
0
7
1
9
2
values.append(
9
)
7
#2
3
#1
15
#4
9
#3
20
#5
node
constructor
values
3
0
7
1
9
2
15
3
20
4
values.append(
20
)
7
#2
3
#1
15
#4
9
#3
20
#5
hasNext()
values
3
0
7
1
9
2
15
3
20
4
index
hasNext() → index 0 < 5 →
true
7
#2
return
3
#1
15
#4
9
#3
20
#5
next()
values
3
0
7
1
9
2
15
3
20
4
index
next() →
7
7
#2
3
#1
15
#4
9
#3
return
20
#5
next()
values
3
0
7
1
9
2
15
3
20
4
index
next() →
9
7
#2
3
#1
15
#4
return
9
#3
20
#5
next()
values
3
0
7
1
9
2
15
3
20
4
index
next() →
15
7
#2
3
#1
15
#4
9
#3
20
#5
return
next()
values
3
0
7
1
9
2
15
3
20
4
next() →
20
7
#2
3
#1
15
#4
9
#3
20
#5
done
values
3
0
7
1
9
2
15
3
20
4
output: [3, 7, 9, 15, 20]
algo
master
.
io
Step:
Start: flatten the whole BST into a sorted array up front
0 / 19
Input
Standard
Complete BST
Sparse BST
Custom
tree
=
[7, 3, 15, null, null, 9, 20]
0 / 19
algo
master
.
io
Step:
Start: flatten the whole BST into a sorted array up front