Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Lowest Common Ancestor of a Binary Search Tree
Bookmark
Generic Tree LCA
BST Recursion
Iterative
Input
Standard (LCA = 6)
Nested (LCA = 2)
Deep (LCA = 5)
Custom
tree
=
[6, 2, 8, 0, 4, 7, 9, null, null, 3, 5]
,
p
=
2
,
q
=
8
p = 2, q = 8
6
2
8
0
4
7
9
3
5
LCA: -
Current
Visited
p and q
Empty side
LCA
p = 2, q = 8
6
2
8
0
4
7
9
3
5
LCA: -
Current
Visited
p and q
Empty side
LCA
p = 2, q = 8
6
2
8
0
4
7
9
3
5
current
Call Stack
lca(6)
LCA: -
Current
Visited
p and q
Empty side
LCA
p = 2, q = 8
6
2
8
0
4
7
9
3
5
current
Call Stack
lca(6)
LCA: -
Current
Visited
p and q
Empty side
LCA
p = 2, q = 8
6
2
8
0
4
7
9
3
5
current
Call Stack
lca(2)
lca(6)
LCA: -
Current
Visited
p and q
Empty side
LCA
p = 2, q = 8
6
2
8
0
4
7
9
3
5
current
Call Stack
lca(6)
LCA: -
Current
Visited
p and q
Empty side
LCA
p = 2, q = 8
6
2
8
0
4
7
9
3
5
current
Call Stack
lca(8)
lca(6)
LCA: -
Current
Visited
p and q
Empty side
LCA
p = 2, q = 8
6
2
8
0
4
7
9
3
5
current
Call Stack
lca(6)
LCA: -
Current
Visited
p and q
Empty side
LCA
p = 2, q = 8
6
2
8
0
4
7
9
3
5
lca
Call Stack
empty
LCA: 6
Current
Visited
p and q
Empty side
LCA
algo
master
.
io
Step:
Find the lowest common ancestor of p=2 and q=8
0 / 7
Input
Standard (LCA = 6)
Nested (LCA = 2)
Deep (LCA = 5)
Custom
tree
=
[6, 2, 8, 0, 4, 7, 9, null, null, 3, 5]
,
p
=
2
,
q
=
8
0 / 7
algo
master
.
io
Step:
Find the lowest common ancestor of p=2 and q=8