Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Step-By-Step Directions From a Binary Tree Node to Another
1
getDirections(Node(5), 3, 6)
Example 1
Simple
Complete Tree
Custom
tree
=
[5, 1, 2, 3, null, 6, 4]
,
startValue
=
3
,
destValue
=
6
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
route: climb up to the LCA, then descend to dest
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
route: climb up to the LCA, then descend to dest
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
LCA =
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
LCA =
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
find the LCA (where the route bends)
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
each step up to the LCA is a
U
U
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
U
reached start:
3
U
U
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
U
U
L
L
L
go left:
+ L
U
U
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
U
U
L
L
dead end: back up
U
U
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
U
U
R
go right:
+ R
U
R
U
L
5
1
2
3
6
4
start
dest
LCA
up (U)
down (L/R)
dirs
U
U
R
L
"UURL"
Step:
Directions from 3 to 6
0 / 35
Example 1
Simple
Complete Tree
Custom
tree
=
[5, 1, 2, 3, null, 6, 4]
,
startValue
=
3
,
destValue
=
6
0 / 35
Step:
Directions from 3 to 6
1
getDirections(Node(5), 3, 6)