Skip to content

Commit e22b5a1

Browse files
committed
Graph Valid Tree
1 parent d858abb commit e22b5a1

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Graph Valid Tree
3+
==================
4+
5+
You have a graph of n nodes labeled from 0 to n - 1. You are given an integer n and a list of edges where edges[i] = [ai, bi] indicates that there is an undirected edge between nodes ai and bi in the graph.
6+
7+
Return true if the edges of the given graph make up a valid tree, and false otherwise.
8+
9+
Example 1:
10+
Input: n = 5, edges = [[0,1],[0,2],[0,3],[1,4]]
11+
Output: true
12+
13+
Example 2:
14+
Input: n = 5, edges = [[0,1],[1,2],[2,3],[1,3],[1,4]]
15+
Output: false
16+
17+
Constraints:
18+
1 <= 2000 <= n
19+
0 <= edges.length <= 5000
20+
edges[i].length == 2
21+
0 <= ai, bi < n
22+
ai != bi
23+
There are no self-loops or repeated edges.
24+
*/
25+
26+
class Solution {
27+
public:
28+
void dfs(int curr, vector<vector<int>>& adj, vector<int>& vis, int& count) {
29+
count++;
30+
for(int child: adj[curr]) {
31+
if(vis[child] == 0) {
32+
vis[child] = 1;
33+
dfs(child, adj, vis, count);
34+
}
35+
}
36+
}
37+
38+
bool validTree(int n, vector<vector<int>>& edges) {
39+
int m = edges.size();
40+
if(m != n-1) return false;
41+
42+
vector<vector<int>> adj(n, vector<int>(0));
43+
44+
for(auto& edge: edges) {
45+
adj[edge[0]].push_back(edge[1]);
46+
adj[edge[1]].push_back(edge[0]);
47+
}
48+
49+
int count = 0;
50+
vector<int> vis(n);
51+
vis[0] = 1;
52+
53+
dfs(0, adj, vis, count);
54+
return count == n;
55+
}
56+
};

Leetcode Daily Challenge/August-2021/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
| 19. | [Maximum Product of Splitted Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3903/) | [cpp](./19.%20Maximum%20Product%20of%20Splitted%20Binary%20Tree.cpp) |
2525
| 20. | [Valid Sudoku](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3904/) | [cpp](./20.%20Valid%20Sudoku.cpp) |
2626
| 21. | [Sudoku Solver](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/615/week-3-august-15th-august-21st/3905/) | [cpp](./20Sudoku%20Solver.%20.cpp) |
27+
| | [Graph Valid Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3906/) | [cpp](./Graph%20Valid%20Tree.cpp) |
2728
| 23. | [Two Sum IV - Input is a BST](https://leetcode.com/explore/featured/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3908/) | [cpp](./23.%20Two%20Sum%20IV%20-%20Input%20is%20a%20BST.cpp) |
2829
| 24. | [Complex Number Multiplication](https://leetcode.com/explore/featured/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3917/) | [cpp](./24.%20Complex%20Number%20Multiplication.cpp) |
2930
| 25. | [Sum of Square Numbers](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3918/) | [cpp](./25.%20Sum%20of%20Square%20Numbers.cpp) |

0 commit comments

Comments
 (0)