1135 Is It A Red-Black Tree (30)自练&大神代码

自练代码:

#include<bits/stdc++.h>  //16分,两个段错误! 
using namespace std;
const int maxn=100;
struct node{
	int data;
	node *rchild,*lchild;
};

int k,pre[maxn]={0},in[maxn]={0},num=0;

node* create(int prel,int prer,int inl,int inr){
	if(prel>prer) return NULL;//递归边界!!
	node* root=new node;
	root->data=pre[prel];
	root->lchild=root->rchild=NULL;
	int k;
	for(k=inl;k<=inr;k++){
		if(in[k]==abs(pre[prel])) break;
	} 
	int numleft=k-inl;//左子树的结点个数!! 
	
	root->lchild=create(prel+1,prel+numleft,inl,k-1);
	root->rchild=create(prel+numleft+1,prer,k+1,inr);
	
	return root;
	
	
}

int getNum(node *root) {  //计算高度(黑结点)   大于0的为黑节点 
    if (root == NULL) return 0;
    int l = getNum(root->lchild);
    int r = getNum(root->rchild);
    return root->data > 0 ? max(l, r) + 1 : max(l, r);
}
bool judge5(node *root) { //判断第五点  递归判断 ! 
    if (root == NULL) return true;
    int l = getNum(root->lchild);
    int r = getNum(root->rchild);
    if(l != r) { return false;}
    return judge5(root->lchild) && judge5(root->rchild);
}
bool judge4(node *root){  //判断第四点  递归判断! 
	if(root==NULL) return true;
	if(root->data<0){
			if(root->lchild!=NULL&&root->lchild->data<0) return false;//必须先把“判断孩子是否为空”写在前面!!!!! 
			if(root->rchild!=NULL&&root->rchild->data<0) return false;//为什么自己心里应该清楚!!!!!!!!!!!!	
		}
	return judge4(root->lchild)&&judge4(root->rchild);  //左右子树均要满足4 
}

int main(){
	scanf("%d",&k);
	for(int i=0;i<k;i++){
		int n;
		scanf("%d",&n);
		for(int j=0;j<n;j++){
			scanf("%d",&pre[j]);
			in[j]=abs(pre[j]);
			if(j==0&&pre[j]<0||(pre[j]==0)){ //1、2、3 
				printf("No\n");
//				printf("PPPP\n");
				break;
			}
		}
		sort(in,in+n);
		node* root=NULL;
		root=create(0,n-1,0,n-1);
		if(judge4(root)==true&&judge5(root)==true) printf("Yes\n");
		else printf("No\n"); 
		fill(pre,pre+maxn,0);
		fill(in,in+maxn,0);
	}
	return 0;
} 



//bool print(node* &root){
//	int is=0;
//	if(root==NULL) return true;
//	if(root->data<0){
//			if((root->lchild->data>0||root->lchild==NULL)&&(root->rchild->data>0||root->rchild==NULL))
//			{
//				is=1;
//			} else {
//				return false;
//			}
//		}
//	print(root->lchild);print(root->rchild);
//}

//void put(node* root){
//	if(root==NULL) return ;
//	put(root->lchild);
//	printf("%d ",root->data);
//	put(root->rchild);
//}

模块自练:

#include<bits/stdc++.h> //红黑树练习最后两个判断函数! 
using namespace std;
const int maxn=200;
struct node{
	int data;
	node *rchild,*lchild;
}; 
bool judge4(node* root){
	if(root==NULL) return true;
	if(root->data<0){
		if(root->lchild!=NULL&&root->lchild->data<0) return false;
		if(root->rchild!=NULL&&root->rchild->data<0) return false;
	}
	return judge4(root->lchild)&&judge4(root->rchild);
}

int getnum(node* root){
	if(root==NULL) return 0;
	int l=getnum(root->lchild);
	int r=getnum(root->rchild);
	return root->data>0 ? max(l,r)+1:max(l,r); 
}

bool judge5(node* root){
	if(root==NULL) return true;
	int l=getnum(root->lchild);
	int r=getnum(root->rchild);
	if(l!=r) return false;
	return judge5(root->lchild)&&judge5(root->rchild);
}

int main(){
	return 0;
}

附上某位大神(薛玉洁)代码:

#include <iostream>  //别人的,个人认为判断bst是多此一举! 
#include <vector>
#include <cmath>
using namespace std;
vector<int> pre, post, 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;
}
void getPost(int root, int end) {
    if (root > end) return;
    int i = root + 1, j = end;
    while (i <= end && pre[root] > pre[i]) i++;
    while (j >= root + 1 && pre[root] <= pre[j]) j--;
    if (i != j + 1) return;
    getPost(root + 1, j);
    getPost(i, end);
    post.push_back(pre[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);
        pre.resize(n);
        node *root = NULL;
        for (int j = 0; j < n; j++) {
            scanf("%d", &arr[j]);
            root = build(root, arr[j]);
            pre[j] = abs(arr[j]);
        }
        post.clear();
        getPost(0, n-1);
        if (post.size() != n || arr[0] < 0 || judge1(root) == false || judge2(root) == false)
            printf("No\n");
        else
            printf("Yes\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值