Learn
Practice
Newsletter
Resources
Mobile
New
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
All Nodes Distance K in Binary Tree
Bookmark
Input
Example 1
Small
Single node
Complete tree
Custom
tree
=
[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]
,
target
=
5
,
k
=
2
dist
0
/ k
2
3
5
target
1
6
2
0
8
7
4
queue
dist
0
/ k
2
3
5
target
1
6
2
0
8
7
4
queue
dist
0
/ k
2
3
5
d0
target
1
6
2
0
8
7
4
5
queue
dequeue
5
dist
0
/ k
2
3
5
d0
target
1
6
2
0
8
7
4
queue
+ parent
3
climbing UP the tree
dist
0
/ k
2
3
d1
5
d0
target
1
6
d1
2
d1
0
8
7
4
6
2
3
front
rear
queue
enqueue
3
distance 1
dist
1
/ k
2
3
d1
5
d0
target
1
6
d1
2
d1
0
8
7
4
2
3
front
rear
queue
neighbors of 6:
left, right,
parent
dist
1
/ k
2
3
d1
5
d0
target
1
6
d1
2
d1
0
8
7
4
3
front / rear
queue
neighbors of 2:
left, right,
parent
dist
1
/ k
2
3
d1
5
d0
target
1
6
d1
2
d1
0
8
7
d2
4
3
7
front
rear
queue
enqueue
7
distance 2
dist
1
/ k
2
3
d1
5
d0
target
1
6
d1
2
d1
0
8
7
d2
4
d2
3
7
4
front
rear
queue
dequeue
3
dist
1
/ k
2
3
d1
5
target
1
6
d1
2
d1
0
8
7
d2
4
d2
7
4
front
rear
queue
5 already visited
(shorter route exists)
dist
2
/ k
2
3
d1
5
d0
target
1
d2
6
d1
2
d1
0
8
7
d2
4
d2
7
4
1
front
rear
queue
return [7, 4, 1]
7
4
1
algo
master
.
io
Step:
Find every node at distance 2 from the target node 5
0 / 34
Input
Example 1
Small
Single node
Complete tree
Custom
tree
=
[3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]
,
target
=
5
,
k
=
2
0 / 34
algo
master
.
io
Step:
Find every node at distance 2 from the target node 5