-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGraphValidTree.cpp
40 lines (34 loc) · 1.12 KB
/
GraphValidTree.cpp
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
#include<vector>
using namespace std;
class Solution {
public:
bool validTree(int n, vector<vector<int>>& edges) {
// Base Case.
if (edges.size() != (n - 1)) return false;
// Step-1: Build adjacency-list.
vector<int> emptyVec;
vector<vector<int>> adjacencyList(n, emptyVec);
for (const auto& edge : edges) {
adjacencyList[edge[0]].push_back(edge[1]);
adjacencyList[edge[1]].push_back(edge[0]);
}
// Step-II: DFS.
unordered_set<int> seen;
return dfs(0, -1, adjacencyList, seen) && seen.size() == n;
}
private:
bool dfs(int node, int parent,
const vector<vector<int>>& adjacencyList, unordered_set<int>& seen) {
if (seen.find(node) != seen.end()) return false;
seen.insert(node);
const vector<int>& neighbors = adjacencyList.at(node);
for (const int neighbor : neighbors) {
if (parent != neighbor) {
if (not dfs(neighbor, node, adjacencyList, seen)) {
return false;
}
}
}
return true;
}
};