PAT甲级1123. Is It a Complete AVL Tree

题意:

在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性。图1-4说明了旋转规则。

现在给出一系列插入,

您应该输出生成的AVL树的级别遍历序列,并告知它是否是完整的二叉树。

输入规格:

每个输入文件包含一个测试用例。对于每种情况,第一行包含正整数N(<= 20)。

一行中的所有数字都以空格分隔。

输出规格:

对于每个测试用例,将键逐个插入到初始空的AVL树中。然后首先在一行中打印生成的AVL树的级别遍历序列。

并且行尾没有额外的空间。然后在下一行中,如果树完成,打印“是”,否则打印“否”。

思路:

AVL.上次做过一道关于AVL的要会写AVL旋转的模板就好了。最后输出是否是平衡二叉树就ok。

ac代码:

C++

  1. // pat1123.cpp : 定义控制台应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include<iostream>
  5. #include<cstdio>
  6. #include<string>
  7. #include<algorithm>
  8. #include<queue>
  9. #include<vector>
  10. #include<cstring>
  11. #include<stdio.h>
  12. #include<map>
  13. #include<cmath>
  14. #include<unordered_map>
  15. #include<unordered_set>
  16. using namespace std;
  17. struct TreeNode
  18. {
  19. int val;
  20. TreeNode* left;
  21. TreeNode* right;
  22. int height;
  23. TreeNode(int x) : val(x) , left(NULL) , right(NULL) , height(0) {}
  24. };
  25. int count_height(TreeNode*& node)
  26. {
  27. if (!node) return -1;
  28. else return node->height;
  29. }
  30. void LL(TreeNode* &root)
  31. {
  32. TreeNode* temp = root->left;
  33. root->left = temp->right;
  34. temp->right = root;
  35. root->height = max(count_height(root->right), count_height(root->left)) + 1;
  36. temp->height = max(count_height(temp->right), count_height(temp->left)) + 1;
  37. root = temp;
  38. }
  39. void RR(TreeNode* &root)
  40. {
  41. TreeNode* temp = root->right;
  42. root->right = temp->left;
  43. temp->left = root;
  44. root->height = max(count_height(root->right), count_height(root->left)) + 1;
  45. temp->height = max(count_height(temp->right), count_height(temp->left)) + 1;
  46. root = temp;
  47. }
  48. void RL(TreeNode* &root)
  49. {
  50. LL(root->right);
  51. RR(root);
  52. }
  53. void LR(TreeNode* &root)
  54. {
  55. RR(root->left);
  56. LL(root);
  57. }
  58. void insert(int val,TreeNode* &root)
  59. {
  60. if (!root)
  61. {
  62. root = new TreeNode(val);
  63. return;
  64. }
  65. if (root->val < val)
  66. {
  67. insert(val, root->right);
  68. if (count_height(root->right) - count_height(root->left) > 1)
  69. {
  70. if (val > root->right->val) RR(root);
  71. else RL(root);
  72. }
  73. }
  74. else
  75. {
  76. insert(val, root->left);
  77. if (count_height(root->left) - count_height(root->right) > 1)
  78. {
  79. if (val > root->left->val) LR(root);
  80. else LL(root);
  81. }
  82. }
  83. root->height = max(count_height(root->left), count_height(root->right)) + 1;
  84. }
  85. int main()
  86. {
  87. int n,val;
  88. scanf("%d", &n);
  89. TreeNode* root = NULL;
  90. for (int i = 0; i < n; i++)
  91. {
  92. scanf("%d", &val);
  93. insert(val, root);
  94. }
  95. //level order traversal
  96. queue<TreeNode*> q;
  97. q.push(root);
  98. vector<TreeNode*> level_order(1,NULL);
  99. while (!q.empty())
  100. {
  101. TreeNode* top = q.front();
  102. q.pop();
  103. level_order.push_back(top);
  104. if (top->left) q.push(top->left);
  105. if (top->right) q.push(top->right);
  106. }
  107. for (int i = 1; i < n; i++)
  108. printf("%d ", level_order[i]->val);
  109. printf("%d\n", level_order[n]->val);
  110. //is complete
  111. bool flag = true;
  112. for (int i = 1; i <= n; i++)
  113. {
  114. if (2 * i <= n && (!level_order[i]->left || level_order[i]->left->val != level_order[2 * i]->val))
  115. {
  116. flag = false;
  117. break;
  118. }
  119. else if(2 * i + 1 <= n && (!level_order[i]->right || level_order[i]->right->val != level_order[2 * i + 1]->val))
  120. {
  121. flag = false;
  122. break;
  123. }
  124. }
  125. if (flag) printf("YES\n");
  126. else printf("NO\n");
  127. return 0;
  128. }

PAT甲级1123. Is It a Complete AVL Tree的更多相关文章

  1. 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 ...

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

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

  3. PAT甲级——A1123 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 ...

  4. PAT Advanced 1123 Is It a Complete AVL Tree (30) [AVL树]

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

  5. 1123 Is It a Complete AVL Tree

    1123 Is It a Complete AVL Tree(30 分) An AVL tree is a self-balancing binary search tree. In an AVL t ...

  6. 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 sub ...

  7. 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 ...

  8. 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 ...

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

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

随机推荐

  1. Codeforces 859E Desk Disorder 并查集找环,乘法原理

    题目链接:http://codeforces.com/contest/859/problem/E 题意:有N个人.2N个座位.现在告诉你这N个人它们现在的座位.以及它们想去的座位.每个人可以去它们想去 ...

  2. MySQL 四种链接

    1.内联接 INNER JOIN(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行. ...

  3. 数据库-mysql函数

    一:MySQL中提供了许多内置函数 CHAR_LENGTH(str) 返回值为字符串str 的长度,长度的单位为字符.一个多字节字符算作一个单字符. 对于一个包含五个二字节字符集, LENGTH()返 ...

  4. Linux基础 - crontab

    列出当前用户设置的定时任务 crontab -l 编辑定时任务 crontab -e 用法 m h dom mon dow * * * * * command 字段详解: *:any m: minut ...

  5. Python线程和进程

    一.进程 程序并不能单独和运行只有将程序装载到内存中,系统为他分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别在于:程序是指令的集合,它是进程的静态描述文本:进程是程序的一次执行活动, ...

  6. Java编程思想第四版第二章练习题答案

    练习1:创建一个类,它包含一个int域和一个char域,它们都没有被初始化.将他们的值打印出来,以验证Java执行了默认初始化 public class JavaThinking { private ...

  7. 聚类:(K-means)算法

    1.归类: 聚类(clustering) 属于非监督学习 (unsupervised learning) 无类别标记(class label) 2.举例: 3. K-means 算法:         ...

  8. ZCTF2015 pwn试题分析

    ZCTF的pwn赛题分析, PWN100 这道题与SCTF的pwn100玩法是一样的,区别在于这个要过前面的几个限制条件.不能触发exit(0).否则就不能实现溢出了. 依然是触发canary来lea ...

  9. C# 在RichTextBox根据内容自动调整高度

    private void richTextBox1_ContentsResized(object sender, ContentsResizedEventArgs e)   {   richTextB ...

  10. Centos 常用命令[持续积累中...]

    CentOS常用到的查看系统命令 uname -a cat /etc/issue /sbin/ifconfig # 查看内核/操作系统/CPU信息 head -n 1 /etc/issue # 查看操 ...