Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
261. Graph Valid Tree
Bookmark
BFS
DFS
Union Find
Input
Valid Tree
Has Cycle
Too Few Edges
Star Shape (Valid)
Custom
n
=
5
,
edges
=
[[0,1],[0,2],[0,3],[1,4]]
edges.length = 4, n - 1 = 4
Input Graph
0
1
2
3
4
BFS Walk
Queue:
empty
Visited:
empty
Legend:
Unreached
Expanding
Reached
edges.length = 4, n - 1 = 4
Input Graph
0
1
2
3
4
BFS Walk
Queue:
empty
Visited:
empty
Legend:
Unreached
Expanding
Reached
edges.length (4) == n - 1 (4) -> OK
Input Graph
0
1
2
3
4
BFS Walk
Queue:
0
Visited:
0
Legend:
Unreached
Expanding
Reached
edges.length (4) == n - 1 (4) -> OK
Input Graph
0
1
2
3
4
BFS Walk
Queue:
empty
Visited:
0
Legend:
Unreached
Expanding
Reached
edges.length (4) == n - 1 (4) -> OK
Input Graph
0
1
2
3
4
BFS Walk
Queue:
1
2
Visited:
0
1
2
Legend:
Unreached
Expanding
Reached
edges.length (4) == n - 1 (4) -> OK
Input Graph
0
1
2
3
4
BFS Walk
Queue:
1
2
3
Visited:
0
1
2
3
Legend:
Unreached
Expanding
Reached
edges.length (4) == n - 1 (4) -> OK
Input Graph
0
1
2
3
4
BFS Walk
Queue:
2
3
Visited:
0
1
2
3
Legend:
Unreached
Expanding
Reached
edges.length (4) == n - 1 (4) -> OK
Input Graph
0
1
2
3
4
BFS Walk
Queue:
2
3
4
Visited:
0
1
2
3
4
Legend:
Unreached
Expanding
Reached
edges.length (4) == n - 1 (4) -> OK
Input Graph
0
1
2
3
4
BFS Walk
Queue:
4
Visited:
0
1
2
3
4
Legend:
Unreached
Expanding
Reached
edges.length (4) == n - 1 (4) -> OK
Input Graph
0
1
2
3
4
BFS Walk
Queue:
empty
Visited:
0
1
2
3
4
Legend:
Unreached
Expanding
Reached
edges.length (4) == n - 1 (4) -> OK
Input Graph
0
1
2
3
4
BFS Walk
Queue:
empty
Visited:
0
1
2
3
4
Valid Tree
Legend:
Unreached
Expanding
Reached
algo
master
.
io
Step:
4 edges matches n - 1, so the edge count is right
0 / 20
Input
Valid Tree
Has Cycle
Too Few Edges
Star Shape (Valid)
Custom
n
=
5
,
edges
=
[[0,1],[0,2],[0,3],[1,4]]
0 / 20
algo
master
.
io
Step:
4 edges matches n - 1, so the edge count is right