-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateBinaryTreeNodes.cpp
More file actions
44 lines (42 loc) · 1.27 KB
/
validateBinaryTreeNodes.cpp
File metadata and controls
44 lines (42 loc) · 1.27 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
// Source: https://leetcode.com/problems/validate-binary-tree-nodes/
// Author: Miao Zhang
// Date: 2021-04-26
class Solution {
public:
bool validateBinaryTreeNodes(int n, vector<int>& leftChild, vector<int>& rightChild) {
vector<int> indegrees(n);
for (auto& l: leftChild) {
if (l >= 0) indegrees[l]++;
}
for (auto& r: rightChild) {
if (r >= 0) indegrees[r]++;
}
int root = -1;
int zeros = 0;
for (int i = 0; i < n; i++) {
if (indegrees[i] > 1) return false;
if (indegrees[i] == 0) {
root = i;
zeros++;
}
}
if (zeros != 1) return false;
vector<int> seen(n);
queue<int> q;
q.push(root);
seen[root] = 1;
while (!q.empty()) {
int cur = q.front();
q.pop();
if (leftChild[cur] != -1 && !seen[leftChild[cur]]) {
q.push(leftChild[cur]);
seen[leftChild[cur]] = 1;
}
if (rightChild[cur] != -1 && !seen[rightChild[cur]]) {
q.push(rightChild[cur]);
seen[rightChild[cur]] = 1;
}
}
return accumulate(begin(seen), end(seen), 0) == n;
}
};