PAT 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.
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
思路:建AVL树,之后进行遍历,出现NULL之后把FLAG标记为FALSE,之后再有非NULL子节点,则不是完全二叉树
using namespace std; struct node{ int val; node *l,*r; }; node* rotateLeft(node *root){ node *t=root->r; root->r=t->l; t->l=root; return t; } node* rotateRight(node *root){ node *t=root->l; root->l=t->r; t->r=root; return t; } node* rotateLeftRight(node *root){ root->l=rotateLeft(root->l); return rotateRight(root); } node* rotateRightLeft(node *root){ root->r=rotateRight(root->r); return rotateLeft(root); } int getH(node *root){ if(root==NULL)return 0; return max(getH(root->l),getH(root->r))+1; } node* insert(node *root,int t){ if(root==NULL){ root=new node(); root->val=t; root->l=NULL; root->r=NULL; return root; } if(t<root->val){ root->l=insert(root->l,t); if(getH(root->l)-getH(root->r)==2){ if(t<root->l->val)root=rotateRight(root); else root=rotateLeftRight(root); } }else{ root->r=insert(root->r,t); if(getH(root->r)-getH(root->l)==2){ if(t>root->r->val)root=rotateLeft(root); else root=rotateRightLeft(root); } } return root; } vector<int> v; bool flag=true,isCom=true; node* levelorder(node *root){ queue<node*> q; q.push(root); while(!q.empty()){ node *t=q.front(); v.push_back(t->val); q.pop(); if(t->l!=NULL){ q.push(t->l); if(!flag)isCom=false; }else flag=false; if(t->r!=NULL){ q.push(t->r); if(!flag)isCom=false; }else flag=false; } } int main() { // freopen("D://dio.txt","r",stdin);// 1099 1064 1114 int n,i; cin>>n; node* root=NULL; int temp; for(i=0;i<n;i++){ scanf("%d",&temp); root=insert(root,temp); } levelorder(root); for(i=0;i<v.size();i++){ if(i==0)cout<<v[i]; else printf(" %d",v[i]); } cout<<endl; if(isCom)cout<<"YES"<<endl; else cout<<"NO"<<endl; return 0; }
标签:node,return,tree,1123,Tree,AVL,NULL,root 来源: https://www.cnblogs.com/xfdmyghdx/p/10433190.html