generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
33 lines (27 loc) · 864 Bytes
/
solution.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
#include <algorithm>
#include <functional>
#include <vector>
class Solution {
public:
bool validateBinaryTreeNodes(int n, std::vector<int>& leftChild, std::vector<int>& rightChild) {
std::vector<bool> exists(n, false);
int count = 0;
leftChild.insert(leftChild.end(), rightChild.begin(), rightChild.end());
for (auto val : leftChild) {
if (val < 0) continue;
if (exists[val]) return false;
exists[val] = true;
++count;
}
if (count != n - 1) return false;
count = 0;
const std::function<void(int)> sweep = [&](int root) {
++count;
if (leftChild[root] >= 0) sweep(leftChild[root]);
if (rightChild[root] >= 0) sweep(rightChild[root]);
};
const int root = std::distance(exists.begin(), find(exists.begin(), exists.end(), false));
sweep(root);
return count == n;
}
};