标签:发展 要求 需要 math 题目 https 本质 判断 res
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
vector<int> arr;
struct node{
int val;
node *L, *R;
};
node* build(node *root, int v){
if(root == NULL){
root = new node;
root->val = v;
root->L = root->R = NULL;
}else if(abs(v) <= abs(root->val)){
root->L = build(root->L, v);
}else{
root->R = build(root->R, v);
}
return root;
}
bool judge1(node* root){
if(root == NULL) return true;
if(root->val < 0){
if(root->L != NULL && root->L->val < 0) return false;
if(root->R != NULL && root->R->val < 0) return false;
}
return judge1(root->L) && judge1(root->R);
}
int getNum(node* root){
if(root == NULL) return 0;
int L = getNum(root->L);
int R = getNum(root->R);
return root->val > 0 ? max(L, R) + 1 : max(L, R);
}
bool judge2(node* root){
if(root == NULL) return true;
int L = getNum(root->L);
int R = getNum(root->R);
if(L != R) return false;
return judge2(root->L) && judge2(root->R);
}
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;
}
A1135 Is It A Red-Black Tree (30分)(红黑树)
标签:发展 要求 需要 math 题目 https 本质 判断 res
原文地址:https://www.cnblogs.com/tsruixi/p/13122068.html