判断AVL树(二叉平衡树)?
  • 板块学术版
  • 楼主封禁用户
  • 当前回复8
  • 已保存回复8
  • 发布时间2023/1/1 12:19
  • 上次更新2023/10/24 05:55:28
查看原帖
判断AVL树(二叉平衡树)?
346332
封禁用户楼主2023/1/1 12:19

题目:

【题目描述】
以左右孩子表示法输入一棵二叉树,请判断其是否为AVL树:
AVL树的性质如下:
1、空树是一个AVL
2、左子树所有结点的值均严格小于根结点的值
3、右子树所有结点的值均严格大于根结点的值
4、左右子树都是AVL(递归定义)
5、不存在任何重复元素
6、左右子树深度差的绝对值不超过1

【输入格式】
一共有T组测试数据,每组测试数据的格式如下:
第一行一个正整数n,表示给定的树的节点的数目,规定节点编号 1~n;
接下来n行,每行4个正整数i、k、L、R,分别表示节点编号、节点的值、左孩子编号、右孩子编号;
如果不存在左 / 右孩子,则以0表示。
每组测试数据之间有一个空白行,不影响输入。
【输出格式】
T行,若当前测试数据为AVL,输出YES,否则输出NO;
注意都是大写字母。

【样例输入】
3

6
1 5 2 3
2 3 4 0
3 7 5 6
4 1 0 0
5 6 0 0
6 8 0 0

6
1 5 2 3
2 3 4 0
3 7 5 6
4 4 0 0
5 6 0 0
6 8 0 0

6
1 5 2 3
2 3 4 0
3 6 0 5
4 1 0 0
5 7 0 6
6 8 0 0

【样例输出】
YES
NO
NO

我的程序:

#include<iostream>
#include<cstring>
using namespace std;
struct node{
	int l,r;
	int value;
	node(int _l=0,int _r=0,int _value=0){
		l=_l;
		r=_r;
		value=_value;
	}
}tree[10001]={};
struct return_node{
	int maxnum;
	int deep;
	bool isok;
	return_node(int _num=0,int _dep=0,bool _ok=true){
		maxnum=_num;
		deep=_dep;
		isok=_ok;
	}
};
int n,t;
bool hasnum[100001]={};
return_node dfs(int i){
//	cout << "i=" << i << ".";
	if(tree[i].l+tree[i].r==0){
//		cout << "In case leaf node!\n";
		return return_node(tree[i].value,1,true);
	}
	if(tree[i].r==0){
		return_node Lt=dfs(tree[i].l);
		if(Lt.isok && Lt.maxnum<tree[i].value && Lt.deep<=1){
//			cout << "Yes!" << i << endl;
//			cout << Lt.deep << ' ' << Lt.isok << ' ' << Lt.maxnum << endl;
			return return_node(tree[i].value,Lt.deep+1,true);
		} else{
//			cout << "Ooops!" << i << endl;
//			cout << Lt.deep << ' ' << Lt.isok << ' ' << Lt.maxnum << endl;
			return return_node(0,0,false);
		}
	} else if(tree[i].l==0){
		return_node Rt=dfs(tree[i].r);
		if(Rt.isok && Rt.maxnum>tree[i].value && Rt.deep<=1){
//			cout << "Yes!" << i << endl;
//			cout << Rt.deep << ' ' << Rt.isok << ' ' << Rt.maxnum << endl;
			return return_node(max(Rt.maxnum,tree[i].value),Rt.deep+1,true);
		} else{
//			cout << "Ooops!" << i << endl;
//			cout << Rt.deep << ' ' << Rt.isok << ' ' << Rt.maxnum << endl;
			return return_node(0,0,false);
		}
	} else{
		return_node Lt=dfs(tree[i].l),Rt=dfs(tree[i].r);
		int minus=(Lt.deep>Rt.deep?Lt.deep-Rt.deep:Rt.deep-Lt.deep);
		if(Lt.isok && Rt.isok && minus<=1 && Lt.maxnum<tree[i].value && Rt.maxnum>tree[i].value){
//		cout << "Yes!" << i << endl;
//		cout << Lt.deep << ' ' << Lt.isok << ' ' << Lt.maxnum << endl;
//		cout << Rt.deep << ' ' << Rt.isok << ' ' << Rt.maxnum << endl;
		return return_node(max(Rt.maxnum,tree[i].value),max(Lt.deep,Rt.deep)+1,true);
	} else{
//		cout << "Ooops!" << i << endl;
//		cout << Lt.deep << ' ' << Lt.isok << ' ' << Lt.maxnum << endl;
//		cout << Rt.deep << ' ' << Rt.isok << ' ' << Rt.maxnum << endl;
		return return_node(0,0,false);
	}
	}
	
}
int main(){
	cin >> t;
	while(t--){
		memset(hasnum,0,sizeof(hasnum));
		for(int i=0;i<10001;i++){
			tree[i]=node();
		}
		cin >> n;
		return_node ans;
		for(int i=0;i<n;i++){
			int f,k,l,r;
			cin >> f >> k >> l >> r;
			if(hasnum[f]){
				cout << "NO\n";
				goto end;
			} else{
				tree[f]=node(l,r,k);
				hasnum[f]=true;
			}
		}
		ans=dfs(1);
		if(ans.isok)	cout << "YES\n";
		else	cout << "NO\n";
		end:;
	}
	return 0;
}

在T较小的时候能对,但是输入的T大了就会错(我也不知道哪里错了)。

2023/1/1 12:19
加载中...