-
Notifications
You must be signed in to change notification settings - Fork 0
/
isItBlackRedTree.cpp
68 lines (61 loc) · 1.37 KB
/
isItBlackRedTree.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
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<vector>
using namespace std;
struct node {
int val;
struct node *l, *r;
};
node* build(int v, node* root){
if(root==NULL){
root = new node();
root->val=v;
root->l=root->r=NULL;
}
else if(abs(v) > abs(root->val)) root->r = build(v, root->r);
else root->l = build(v, root->l);
return root;
}
bool judgeC(node* root){
if(root==NULL) return false;
if(root->val< 0){
if(root->r!=NULL){
return root->r->val>0?false:true;
}
if(root->l!=NULL){
return root->l->val>0?false:true;
}
}
return judgeC(root->l) || judgeC(root->r);
}
int getNum(node* root){
if(root==NULL) return 0;
int r = getNum(root->r);
int l = getNum(root->l);
return root->val>0?max(l, r) + 1:max(l, r);
}
bool judgeB(node* root){
if(root==NULL) return false;
int l = getNum(root->l);
int r = getNum(root->r);
return (l!=r) || judgeB(root->l) || judgeB(root->r);
}
int main(){
int cs,nl;
cin>>cs;
for(int i=0; i<cs; i++){
cin>>nl;
node* root=NULL;
int val;
for(int j=0;j<nl;j++){
cin>>val;
root = build(val, root);
}
if(root->val<0||judgeC(root)||judgeB(root)){
cout<<"No"<<endl;
}
else
{
cout<<"Yes"<<endl;
}
}
}