-
Notifications
You must be signed in to change notification settings - Fork 3
/
1361.cpp
43 lines (41 loc) · 1.41 KB
/
1361.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
41
42
43
class Solution {
public:
bool traverse(int node, vector<int>& leftChild, vector<int>& rightChild, vector<bool>& visited
) {
if (node == -1) return true;
if (visited[node]) return false;
visited[node] = true;
bool left = traverse(leftChild[node], leftChild, rightChild, visited);
bool right = traverse(rightChild[node], leftChild, rightChild, visited);
return left && right;
}
bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {
vector<int> parents(n, -1);
for (int i = 0; i < n; ++i) {
int child = leftChild[i];
if (child != -1) {
if (parents[child] != -1) return false;
parents[child] = i;
}
child = rightChild[i];
if (child != -1) {
if (parents[child] != -1) return false;
parents[child] = i;
}
}
// find root
int cnt = 0;
int current = 0;
while (parents[current] != -1) {
current = parents[current];
cnt++;
if (cnt > 2 * n) return false;
}
// traverse
vector<bool> visited(n, false);
bool isTree = traverse(current, leftChild, rightChild, visited);
int nodeCnt = 0;
for (int i = 0; i < n; ++i) nodeCnt += visited[i];
return isTree && (nodeCnt == n);
}
};