Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Kth Smallest Element in a BST
Bookmark
Store Values
Recursive Stop
Iterative Stack
Input
Example 1
Example 2
Example 3
Custom
tree
=
[3, 1, 4, null, 2]
,
k
=
1
3
1
4
2
k = 1
sorted
inorder visits BST values in ascending order
3
1
4
2
k = 1
sorted
inorder visits BST values in ascending order
3
1
#1
curr
4
2
k = 1
sorted
1
1st
1
is the 1st smallest
3
1
#1
4
2
#2
curr
k = 1
sorted
1
1st
2
2nd
2
is the 2nd smallest
3
#3
curr
1
#1
4
2
#2
k = 1
sorted
1
1st
2
2nd
3
3rd
3
is the 3rd smallest
3
#3
1
#1
4
#4
curr
2
#2
k = 1
sorted
1
1st
2
2nd
3
3rd
4
4th
4
is the 4th smallest
3
#3
1
#1
4
#4
2
#2
k = 1
sorted
1
1st
2
2nd
3
3rd
4
4th
all values collected, take index
k - 1 = 0
3
#3
1
#1
curr
4
#4
2
#2
k = 1
sorted
1
1st
2
2nd
3
3rd
4
4th
values[0] =
1
3
#3
1
#1
4
#4
2
#2
k = 1
sorted
1
1st
2
2nd
3
3rd
4
4th
return 1
algo
master
.
io
Step:
Start: an inorder traversal lists BST values in ascending order
0 / 7
Input
Example 1
Example 2
Example 3
Custom
tree
=
[3, 1, 4, null, 2]
,
k
=
1
0 / 7
algo
master
.
io
Step:
Start: an inorder traversal lists BST values in ascending order