-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphValidTree.java
More file actions
60 lines (50 loc) · 1.82 KB
/
GraphValidTree.java
File metadata and controls
60 lines (50 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {
public boolean validTree(int n, int[][] edges) {
// A tree must have exactly n - 1 edges
if (edges.length != n - 1) return false;
// Construct the adjacency matrix
int[][] graph = new int[n][n];
for (int[] edge : edges) {
graph[edge[0]][edge[1]] = 1;
graph[edge[1]][edge[0]] = 1;
}
// If the graph is both connected and acyclic, it's a tree
return isConnected(graph) && !containsCycle(graph);
}
private boolean containsCycle(int[][] graph) {
boolean[] visited = new boolean[graph.length];
// Since it's an undirected graph, check for cycles from any node
return hasCycle(graph, visited, 0, -1);
}
private boolean hasCycle(int[][] graph, boolean[] visited, int node, int parent) {
visited[node] = true;
for (int i = 0; i < graph.length; i++) {
if (graph[node][i] == 1) { // If there's an edge
if (!visited[i]) {
if (hasCycle(graph, visited, i, node)) return true;
} else if (i != parent) {
return true; // If visited and not parent, cycle detected
}
}
}
return false;
}
private boolean isConnected(int[][] graph) {
boolean[] visited = new boolean[graph.length];
// Start DFS from node 0
dfs(graph, visited, 0);
// Check if all nodes are visited
for (boolean v : visited) {
if (!v) return false;
}
return true;
}
private void dfs(int[][] graph, boolean[] visited, int node) {
visited[node] = true;
for (int i = 0; i < graph.length; i++) {
if (graph[node][i] == 1 && !visited[i]) {
dfs(graph, visited, i);
}
}
}
}