题目

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

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

题目分析

已知平衡二叉树的建树序列,求二叉平衡树的层序序列,并判断是否是完全二叉树

解题思路

  1. 平衡二叉树建树(左右旋)
  2. 判断是否是二叉树

    思路1:记录每个节点的index,根节点index=i,左子节点index=2i+1,右子节点index=2i+2。判断最后一个节点的index==n-1,相等即为完全二叉树

    思路2:出现过NULL值后,若不再出现非NULL节点,即为完全二叉树

Code

Code 01

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
struct node {
int data;
int heigh=0;
int index=-1;
node * left=NULL;
node * right=NULL;
node() {}
node(int _data):data(_data) {
heigh=1;
}
};
int getHeigh(node * root) {
if(root==NULL)return 0;
else return root->heigh;
}
int getBalanceFactor(node * root) {
return getHeigh(root->left)-getHeigh(root->right);
}
void updateHeigh(node * root) {
root->heigh=max(getHeigh(root->left),getHeigh(root->right))+1;
}
void L(node * &root) {
node * temp=root->right;
root->right=temp->left;
temp->left = root;
updateHeigh(root);
updateHeigh(temp);
root=temp;
}
void R(node * &root) {
node * temp=root->left;
root->left=temp->right;
temp->right=root;
updateHeigh(root);
updateHeigh(temp);
root = temp;
}
void insert(node * &root, int val) {
if(root==NULL) {
root=new node(val);
return;
}
if(val<root->data) {
insert(root->left,val);
updateHeigh(root);
if(getBalanceFactor(root)==2) {
if(getBalanceFactor(root->left)==1) {
R(root);
} else if (getBalanceFactor(root->left)==-1) {
L(root->left);
R(root);
}
}
} else {
insert(root->right,val);
updateHeigh(root);
if(getBalanceFactor(root)==-2) {
if(getBalanceFactor(root->right)==-1) {
L(root);
} else if (getBalanceFactor(root->right)==1) {
R(root->right);
L(root);
}
}
}
}
vector<node*>nds;
/*
思路一:使用index判断是否完全二叉树
*/
//void levelOrder(node * root){
// queue<node*> q;
// root->index = 0;
// q.push(root);
// while(!q.empty()){
// node * now = q.front();
// q.pop();
// nds.push_back(now);
// if(now->left!=NULL){
// now->left->index=now->index*2+1;
// q.push(now->left);
// }
// if(now->right!=NULL){
// now->right->index=now->index*2+2;
// q.push(now->right);
// }
// }
//}
/*
思路二:出现NULL后不再出现非NULL节点即为完全二叉树
*/
int isComplete = 1, after = 0;
void levelOrder(node *tree) {
queue<node *> queue;
queue.push(tree);
while (!queue.empty()) {
node *temp = queue.front();
queue.pop();
nds.push_back(temp);
if (temp->left != NULL) {
if (after) isComplete = 0;
queue.push(temp->left);
} else {
after = 1;
}
if (temp->right != NULL) {
if (after) isComplete = 0;
queue.push(temp->right);
} else {
after = 1;
}
}
}
bool cmp(node * &n1,node * &n2) {
return n1->index<n2->index;
}
int main(int argc,char * argv[]) {
int n,m;
scanf("%d",&n);
node * root=NULL;
for(int i=0; i<n; i++) {
scanf("%d",&m);
insert(root,m);
}
levelOrder(root);
for(int i=0; i<nds.size(); i++) {
printf("%d",nds[i]->data);
if(i!=nds.size()-1)printf(" ");
else printf("\n");
}
// 思路一:使用index判断是否完全二叉树
// if(nds[n-1]->index==n-1) printf("YES");
// else printf("NO"); // 思路二:出现NULL后不再出现非NULL节点即为完全二叉树
if(isComplete) printf("YES");
else printf("NO");
return 0;
}

PAT Advanced 1123 Is It a Complete AVL Tree (30) [AVL树]的更多相关文章

  1. PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6806292.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  2. PAT (Advanced Level) 1099. Build A Binary Search Tree (30)

    预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath& ...

  3. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  4. PAT甲级——1123 Is It a Complete AVL Tree (完全AVL树的判断)

    嫌排版乱的话可以移步我的CSDN:https://blog.csdn.net/weixin_44385565/article/details/89390802 An AVL tree is a sel ...

  5. 1123. Is It a Complete AVL Tree (30)

    An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child sub ...

  6. PAT甲级1123 Is It a Complete AVL Tree【AVL树】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805351302414336 题意: 给定n个树,依次插入一棵AVL ...

  7. PAT 1123. Is It a Complete AVL Tree (30)

    AVL树的插入,旋转. #include<map> #include<set> #include<ctime> #include<cmath> #inc ...

  8. PAT甲级题解-1066. Root of AVL Tree (25)-AVL树模板题

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6803291.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  9. PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

    简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

随机推荐

  1. 如何拯救被Due逼疯的留学生们?

    Final季又到了,还有多少paper,多少project没完成?每年一到这个时候,手忙脚乱赶各种进度就成了留学小伙伴们共同的日常.任务多,不知道从何开始,拖延,烦躁……到底该怎么办?小编今天为各位介 ...

  2. Spark 下操作 HBase(1.0.0 新 API)

    hbase1.0.0版本提供了一些让人激动的功能,并且,在不牺牲稳定性的前提下,引入了新的API.虽然 1.0.0 兼容旧版本的 API,不过还是应该尽早地来熟悉下新版API.并且了解下如何与当下正红 ...

  3. C++面试常见问题——09static关键字

    static关键字 局部变量 局部变量 局部变量是最常见的量,编译器不会对其进行初始化,除非对其显式赋值,不然初始值是随机的. 普通局部变量存储在栈空间,使用完毕后会立即被释放. 静态局部变量 静态局 ...

  4. nodejs学习笔记(一):centos7安装node环境

    由于windows环境安装nodejs只需要访问官方网站下载压缩包,解压即可. 首先检查自己是否安装==wget==,已安装可以跳过这步,未安装则需要先安装: linux yum install -y ...

  5. Bug 佛祖镇楼

    原文链接:https://www.cnblogs.com/xdp-gacl/p/4198935.html // _ooOoo_ // o8888888o // 88" . "88 ...

  6. 一百一十四、SAP查看事务代码对应工程源码

    一.比如我们想看ZMMR008的源码,输入事务代码,点击显示 二.点击显示之后,在程序这儿,的双击打开 三.可以看到源码内容

  7. 004、mysql使用SHOW语句找出服务器上当前存在什么数据库

    SHOW DATABASES; 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477247)哦,谢谢.

  8. Pyinstaller的安装及简单使用

    (1)安装: 用传统的pip install pyinstaller出错,在https://pypi.org/project/PyInstaller/#files上下载PyInstaller-3.4. ...

  9. Node.js 文件系统模块

    章节 Node.js 介绍 Node.js 入门 Node.js 模块 Node.js HTTP模块 Node.js 文件系统模块 Node.js URL模块 Node.js NPM Node.js ...

  10. 每天一点点之css - 动画-一个圆绕着另一个圆动(绕着轨迹运动)

    最近要开发一个类似星河的效果,需要小圆绕着一定的轨迹运动,这个时候我首先想到的是使用canvas来实现,在实现过程中发现这个实现起来不是很灵活,然后想到css3有动画也可以实现,下面是效果 注:图2是 ...