-
Notifications
You must be signed in to change notification settings - Fork 814
/
Copy path1135. Is It A Red-Black Tree (30).cpp
59 lines (59 loc) · 1.54 KB
/
1135. Is It A Red-Black Tree (30).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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<int> arr;
struct node {
int val;
struct node *left, *right;
};
node* build(node *root, int v) {
if(root == NULL) {
root = new node();
root->val = v;
root->left = root->right = NULL;
} else if(abs(v) <= abs(root->val))
root->left = build(root->left, v);
else
root->right = build(root->right, v);
return root;
}
bool judge1(node *root) {
if (root == NULL) return true;
if (root->val < 0) {
if (root->left != NULL && root->left->val < 0) return false;
if (root->right != NULL && root->right->val < 0) return false;
}
return judge1(root->left) && judge1(root->right);
}
int getNum(node *root) {
if (root == NULL) return 0;
int l = getNum(root->left);
int r = getNum(root->right);
return root->val > 0 ? max(l, r) + 1 : max(l, r);
}
bool judge2(node *root) {
if (root == NULL) return true;
int l = getNum(root->left);
int r = getNum(root->right);
if(l != r) return false;
return judge2(root->left) && judge2(root->right);
}
int main() {
int k, n;
scanf("%d", &k);
for (int i = 0; i < k; i++) {
scanf("%d", &n);
arr.resize(n);
node *root = NULL;
for (int j = 0; j < n; j++) {
scanf("%d", &arr[j]);
root = build(root, arr[j]);
}
if (arr[0] < 0 || judge1(root) == false || judge2(root) == false)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}