Root of AVL Tree (25)
来源:互联网 发布:skype网络电话机 编辑:程序博客网 时间:2024/04/28 05:12
题目地址:http://www.patest.cn/contests/pat-a-practise/1066
需要重点掌握 平衡二叉树的4种旋转(LL RR LR RL )
注意树高度要跟着调整
学习网址:
http://www.cnblogs.com/huangxincheng/archive/2012/07/22/2603956.html
http://www.cnblogs.com/guyan/archive/2012/09/03/2668399.html
/*Root of AVL Tree (25)http://www.patest.cn/contests/pat-a-practise/1066*/#include <cstdio> #include <sstream> #include <cstring> #include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <cmath>#include <set>#include <map>#include <unordered_map>#include <stack>using namespace std;int n;typedef struct node{ int data; int height; struct node* left; struct node* right; node(int _data = -1) { data = _data; height = 0; left = NULL; right = NULL; }}Bnode;int max2(int a, int b){ return a > b ? a : b;}int getHeight(Bnode* root){ if (root == NULL) return 0; //return -1 也行 return root->height;}bool isbalance(Bnode* root){ int k = abs(getHeight(root->left) - getHeight(root->right)); if (k < 2) return true; return false;}// 调整高度 : 左子树的高度 右子树的高度取最大 加上1 就行了 int UpdateHeight(Bnode* root){ return max2(getHeight(root->left), getHeight(root->right)) + 1;}Bnode* LL(Bnode* root){ Bnode* tmpL = root->left; root->left = tmpL->right; tmpL->right = root; root->height = UpdateHeight(root);//先调整root , 再调整 tmpL tmpL->height = UpdateHeight(tmpL); return tmpL;}Bnode* RR(Bnode* root){ Bnode* tmpL = root->right; root->right = tmpL->left; tmpL->left = root; root->height = UpdateHeight(root); tmpL->height = UpdateHeight(tmpL); return tmpL;}Bnode* LR(Bnode* root){ Bnode* tmpL = root->left; root->left = RR(tmpL); return LL(root);}Bnode* RL(Bnode* root){ Bnode* tmpL = root->right; root->right = LL(tmpL); return RR(root);}Bnode* createTree(Bnode* root, int data){ if (root != NULL) { if (data < root->data) // left { root->left = createTree(root->left, data); if (!isbalance(root)) { if (data < root->left->data) // LL { root = LL(root); } else{ // LR root = LR(root); } } } else{ // right root->right = createTree(root->right, data); if (!isbalance(root)) { if (data > root->right->data) // RR { root = RR(root); } else{ // RL root = RL(root); } } } } else{ root = new node(data); } root->height = UpdateHeight(root); //max2(getHeight(root->left), getHeight(root->right)) + 1; return root;}int main(){ //freopen("in", "r", stdin); while (scanf("%d", &n) != EOF) { Bnode *root = NULL; int i, tmp; root = NULL; for (i = 0; i < n; i++) { scanf("%d", &tmp); root = createTree(root, tmp); } cout << root->data << endl; } return 0;}
0 0
- Root of AVL Tree (25)
- Root of AVL Tree
- Root of AVL Tree
- Root of AVL Tree
- 【PAT】1066. Root of AVL Tree (25)
- 【PAT】1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- 04-1. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- PAT1066--Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- PAT 1066. Root of AVL Tree (25)
- PAT 1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- 1066. Root of AVL Tree (25)
- 网站的脉络梳理
- android源码学习规划
- 自定义注解与mysql整合过滤查询实现
- 多态详解
- eclipse通过Remote Java Application调试Uiautomator的方法
- Root of AVL Tree (25)
- JPA相关注解
- 8-18 UIButton UImageView UIView
- android Dialog和EditText
- Myeclipse快捷键介绍
- 将正在运行的项目war包重命名后再恢复原名造成问题的疑问
- 关于volley的一点自己的理解
- JPA测试实例
- 算菜价