|
1 | 1 | packagecom.fishercoder.solutions;
|
2 | 2 |
|
3 |
| -/** |
4 |
| - *261. Graph Valid Tree |
5 |
| - * |
6 |
| - * Given n nodes labeled from 0 to n - 1 and a list of undirected edges |
7 |
| - * (each edge is a pair of nodes), |
8 |
| - * write a function to check whether these edges make up a valid tree. |
9 |
| -
|
10 |
| - For example: |
11 |
| -
|
12 |
| - Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true. |
13 |
| -
|
14 |
| - Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false. |
15 |
| -
|
16 |
| - Hint: |
17 |
| -
|
18 |
| - Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], what should your return? Is this case a valid tree? |
19 |
| - According to the definition of tree on Wikipedia: |
20 |
| - “a tree is an undirected graph in which any two vertices are connected by exactly one path. |
21 |
| - In other words, any connected graph without simple cycles is a tree.” |
22 |
| -
|
23 |
| - Note: you can assume that no duplicate edges will appear in edges. |
24 |
| - Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges. |
25 |
| - */ |
26 | 3 | publicclass_261 {
|
27 | 4 |
|
28 | 5 | publicstaticclassSolution1 {
|
|