1123. Is It a Complete AVL Tree (30)
来源:互联网 发布:盲僧李小龙皮肤淘宝 编辑:程序博客网 时间:2024/05/17 22:15
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 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.
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:588 70 61 63 65Sample Output 1:
70 63 88 61 65YESSample Input 2:
888 70 61 96 120 90 65 68Sample Output 2:
88 65 96 61 70 90 120 68NO
#include <cstdio>#include <vector>#include <queue>#include <iostream>using namespace std;struct Node{ struct Node *left,*right; int val;};int getHeight(struct Node * tree){ if(tree==NULL) return 0; int l=getHeight(tree->left); int r=getHeight(tree->right); return max(l,r)+1;}struct Node *leftRotate(struct Node *tree){ struct Node * tmp =tree->right; tree->right=tmp->left; tmp->left=tree; return tmp;};struct Node* rightRotate(struct Node *tree){ struct Node *tmp=tree->left; tree->left=tmp->right; tmp->right=tree; return tmp;};struct Node* leftRightRotate(struct Node * tree){ tree->left =leftRotate(tree->left); tree=rightRotate(tree); return tree;};struct Node* rightLeftRotate(struct Node *tree){ tree->right=rightRotate(tree->right); tree=leftRotate(tree); return tree;};struct Node * Insert( struct Node *tree,int val ){ if(tree==NULL){ tree=new Node(); tree->left=NULL; tree->right=NULL; tree->val=val; return tree; } if(tree->val > val ){ tree->left=Insert(tree->left,val); int l=getHeight(tree->left); int r=getHeight(tree->right); if(l-r>=2){ if(tree->left->val >val ) { tree=rightRotate(tree); }else tree=leftRightRotate(tree); } }else{ tree->right=Insert(tree->right,val); int l=getHeight(tree->left); int r=getHeight(tree->right); if(r-l>=2) if(tree->right->val < val) tree=leftRotate(tree); else tree=rightLeftRotate(tree); } return tree;};int yes=1;void levelOrder(struct Node *tree){ queue<struct Node* > q; int after=0; q.push(tree); vector<int>ans; while(!q.empty()){ struct Node* tmp=q.front(); q.pop(); ans.push_back(tmp->val); if( tmp->left ){ q.push(tmp->left); if(after) yes=0; }else after=1; if(tmp->right){ q.push(tmp->right); if(after) yes=0; }else after=1; } cout<<ans[0]; for(int i=1;i<ans.size();i++) cout<<" "<<ans[i]; cout<<endl; if(yes) cout<<"YES"<<endl; else cout<<"NO"<<endl; return ;}int main(){ int n; cin>>n; struct Node * tree=NULL; for(int i=1;i<=n;i++){ int x; cin>>x; tree=Insert(tree,x); } levelOrder(tree);}
- 1123. Is It a Complete AVL Tree
- PAT 1123. Is It a Complete AVL Tree (30)
- PAT--1123. Is It a Complete AVL Tree (30)
- 1123. Is It a Complete AVL Tree (30)
- 1123. Is It a Complete AVL Tree (30)
- 1123. Is It a Complete AVL Tree (30)
- 1123. Is It a Complete AVL Tree (30)解题报告
- 1123. Is It a Complete AVL Tree (30)
- pat 1123. Is It a Complete AVL Tree (30)
- 1123. Is It a Complete AVL Tree (30) PAT 甲级
- 1123. Is It a Complete AVL Tree (30)
- PAT_A 1123. Is It a Complete AVL Tree (30)
- 1123.Is It a Complete AVL Tree (30)
- PAT 1123. Is It a Complete AVL Tree (30)
- 1123. Is It a Complete AVL Tree (30)
- PAT 甲级 1123. Is It a Complete AVL Tree (30)
- 1123. Is It a Complete AVL Tree (30)
- 1123. Is It a Complete AVL Tree (30)
- 交换机和路由器的区别
- 超详细理解:kmp算法next数组求解过程和回溯的含义
- 分布式系统设计原则
- SRE|当Google的核心准则遇到Xero的最佳实践
- 【算法优化】比较器的使用
- 1123. Is It a Complete AVL Tree (30)
- git切换远程库 创建分支并提交到远程
- 抽象类和接口
- redis的分布式解决方式--codis (转)
- Java读取文件夹下的文件并进行处理
- Java 中使用 Redis
- js解析Excel文件,解析后的数据用Echarts折线图展示
- GIT command usage
- 资料