1123 Is It a Complete AVL Tree

An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.

F1.jpg F2.jpg
F3.jpg F4.jpg
Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤ 20). Then N distinct integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:
For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print YES if the tree is complete, or NO if not.

Sample Input 1:
5
88 70 61 63 65
Sample Output 1:
70 63 88 61 65
YES
Sample Input 2:
8
88 70 61 96 120 90 65 68
Sample Output 2:
88 65 96 61 70 90 120 68
NO
做这道题建议先做A1110和A1066这两道,这道题其实就是这两道的结合版本,大佬请无视 如果前面两个题目会做了,理解了,这道题也就水到渠成了

自己的问题
模板还是太生了,或者说理解还是不够深刻,昨天刚背的模板,今天打的比较生疏,有一点还搞错了

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
struct node{
	int v,height;
	node *lchild,*rchild;
}*root;
int n;
node* newnode(int v){
	node* Node = new node;
	Node->v = v;
	Node->height=1;
	Node->lchild = Node->rchild =NULL;
	return Node;
}
int getheight(node *root){
	if(root == NULL) return 0;
	return root->height;
}
void updateheight(node *root){
	root->height = max(getheight(root->lchild),getheight(root->rchild)) + 1;
}
int getbalancefactor(node* root){
	return getheight(root->lchild) - getheight(root->rchild);
}
void L(node* &root){
	node* temp =root->rchild;
	root->rchild=temp->lchild;
	temp->lchild=root;
	updateheight(root);
	updateheight(temp);
	root = temp;
}
void R(node* &root){
	node* temp = root->lchild;
	root->lchild = temp->rchild;
	temp->rchild = root;
	updateheight(root);
	updateheight(temp);
	root =temp;
}
void insert(node* &root,int v){
	if(root == NULL){
		root  = newnode(v);
		return;
	}
	if(v < root->v){
		insert(root->lchild,v);  
		updateheight(root);
		if(getbalancefactor(root) == 2){//L型 
			if(getbalancefactor(root->lchild) == 1) R(root);
			else if(getbalancefactor(root->lchild) == -1){
				L(root->lchild);
				R(root);
			}
		}
	}
	else {
	       insert(root->rchild,v);
		   updateheight(root);
		if(getbalancefactor(root) == -2) {//R型 
			if(getbalancefactor(root->rchild) == -1){
				L(root);
			} 
			else if(getbalancefactor(root->rchild) == 1){
				R(root->rchild);
				L(root);
			}
		}
    }   
}
int bk,flag;
void bfs(node* root){
	queue<node*> q;
	q.push(root);
	int bkk=0;
	while(!q.empty()){
		node* temp= q.front();
		q.pop();
		bk++;
		if(bk < n) cout<<temp->v<<' ';
		else cout<<temp->v;
		if(bkk){
			if   (  !(temp->lchild == NULL && temp->rchild == NULL) ) flag = 1;
		}
		if(temp->lchild == NULL && temp->rchild !=NULL) flag=1;
		if(temp->rchild == NULL) bkk = 1;
		if(temp->lchild !=NULL) q.push(temp->lchild);
		if(temp->rchild !=NULL) q.push(temp->rchild);
	}
}
int main(){
	cin>>n;
	int x;
	for(int i=0;i<n;i++){
		cin>>x;
		insert(root,x);
	}
	bfs(root);
	cout<<endl;
	if(!flag) cout<<"YES";
	else cout<<"NO";
	return 0;
}

题外话,再做这道题目的时候,我的同学拉着我报名CCF,我是知道CCF,有个厉害的学长以前就考了,嘛,我是觉得自己比较菜就不想报,同学说反正也没啥损失,毕竟自由的((●ˇ∀ˇ●),免费的),那就试试吧,但是要报名就要考的像样一点
接下来就准备3场考试
1.PAT甲级
2.CCF
3.cet 6

===================================================================
1对于甲级现在又5 6 成能过吧,当然过是不行的,自己得要求高点,85,90吧这个亚子,不然低要求肯定过不了,就目前刷题状况来说,先说我自己的学习模式,我是比较喜欢套模板,然后再考虑,所以碰到自己不熟悉的就拉跨,所以还是要精益求精

2.CCF 刚才也网上也看了看别人的CCF试后的总结,感觉和PAT不大一样,而且比甲级也要难,对时间复杂度要去也很高,对STL比较排斥吧 = =,感觉甲级喜欢STL,还有像图论里面的最小生成树,bell-man,Floyd等等,图论我就只回一个dijkstra,(写过题目的,现在也忘记的差不多了,最近一直在搞树),就是算法笔记上的有些东西我看甲级不怎么考就没怎么弄,这是不对的= =唉,目标300分吧
3.cet 6 这次把分刷高一点,暂定500吧,开学到现在没碰过
4.还有算法课上的东西,像线段树,LCA,最小流 哇,头疼
5.颓废了大半年了,这个月先努力试试看,同学厉害的都拿铜牌了= =,别人是真的厉害,自己也要好好努力了,然后过年也要考虑考研的事情了,或者蓝桥杯什么的

总之,要努力,不要再颓废,自卑了,每天再实验室呆到晚上9:30吧至少,fighting!!! 唉

发布了139 篇原创文章 · 获赞 3 · 访问量 8509

猜你喜欢

转载自blog.csdn.net/weixin_44270812/article/details/103016494