A1066

news/2024/10/23 7:32:21/

没写出,平衡二叉树需要多练多思考多画图.
注意点:
1、有可能并不是根结点的左右子树未平衡,遇到这种情况时,做平衡操作后,需要让该节点的父亲结点指向更新后的孩子结点,以保证树中间不断掉.
通过它们实现:
root->left = insert(root->left, val);
root->right = insert(root->right, val);
每次递归结束都会返回当前根结点,这样在递归回来时,当前根结点便会自然的指向已更新后的左孩子与右孩子(即上一递归结束返回过来的结点).
2、若根结点的左右子树未平衡,则平衡操作后,根结点会更换,因此insert()函数每次的调用都应将最新的根结点返回出来,以保证下次作插入操作时,树是完整的.

//柳神 
#include <iostream>
using namespace std;
struct node {int val;struct node *left, *right;
};
node *rotateLeft(node *root) {node *t = root->right;root->right = t->left;t->left = root;return t;
}
node *rotateRight(node *root) {node *t = root->left;root->left = t->right;t->right = root;return t;
}
node *rotateLeftRight(node *root) {root->left = rotateLeft(root->left);return rotateRight(root);
}
node *rotateRightLeft(node *root) {root->right = rotateRight(root->right);return rotateLeft(root);
}
int getHeight(node *root) {                               //每次通过递归获取高度 if(root == NULL) return 0;return max(getHeight(root->left), getHeight(root->right)) + 1;            
}
node *insert(node *root, int val) {if(root == NULL) {root = new node();root->val = val;root->left = root->right = NULL;} else if(val < root->val) {root->left = insert(root->left, val);//递归回来时,若根结点的左子树做了修改,返回回来的一定是最新的左子结点. if(getHeight(root->left) - getHeight(root->right) == 2)                       root = val < root->left->val ? rotateRight(root) : rotateLeftRight(root);//通过比较目标值与root的左孩子值,确定是LL/LR } else {root->right = insert(root->right, val);//递归回来时,若根结点的右子树做了修改,返回回来的一定是最新的右子结点. if(getHeight(root->left) - getHeight(root->right) == -2)root = val > root->right->val ? rotateLeft(root) : rotateRightLeft(root);//通过比较目标值与root的左孩子值,确定是RL/RR}return root;                        //插入完成后返回root指针赋值给main函数里的root 
}
int main() {#ifdef ONLINE_JUDGE#elsefreopen("1.txt","r",stdin);#endifint n, val;scanf("%d", &n);node *root = NULL;for(int i = 0; i < n; i++) {scanf("%d", &val);root = insert(root, val);}printf("%d", root->val);return 0;
}

http://www.ppmy.cn/news/323926.html

相关文章

PAT A1068

题目&#xff1a;https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 我自己没编出来。我的思路是数组从小到大排序&#xff0c;然后算dp[i][j]&#xff08;第i位最大量为j的钱&#xff09;&#xff0c;得到dp后遍历所有dp[i]&#xff0c;找dp[i…

A1065 A+B and C (64bit)

1065 AB and C (64bit) &#xff08;20 分) Given three integers A, B and C in [−​​,], you are supposed to tell whether AB>C. Input Specification: The first line of the input gives the positive number of test cases, T (≤10). Then T test cases follow…

PAT a1128

目的:判断是不是Queens Puzzle 输入&#xff1a; K 例子个数&#xff0c;[1 200] N 图片大小&#xff0c;[4 1000] 输出&#xff1a; 是的输出&#xff0c;YES 不是输出&#xff0c;NO 算法&#xff1a; 不在一行好判断&#xff0c;不在一条斜线上&#xff0c;在一条…

1286

找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5697 Accepted Submission(s): 2940 Problem Description 新年快到了&#xff0c;“猪头帮协会”准备搞一个聚会&#xff0c;已经知道现有会员N人&a…

洛谷 P1866 编号

题目描述 太郎有N只兔子&#xff0c;现在为了方便识别它们&#xff0c;太郎要给他们编号。兔子们向太郎表达了它们对号码的喜好&#xff0c;每个兔子i想要一个整数&#xff0c;介于1和Maxnumber[i]之间&#xff08;包括1和Maxnumber[i]&#xff09;。当然&#xff0c;每个兔子…

全程15分钟 详解如何为MacBook Pro 15寸 加装固态硬

本文导航 第1页&#xff1a;详解如何为MBP15加装固态硬第2页&#xff1a;用时约3分钟&#xff1a;查看硬盘和光驱接口信息第3页&#xff1a;用时约2分钟&#xff1a;拆解机身底盖螺丝 第4页&#xff1a;用时约3分钟&#xff1a;拆解光驱和清理风扇第5页&#xff1a;用时约5分…

A1062(25)

德才论 看题目可能会觉得简单&#xff0c;但是实际的话会有些麻烦。注意根据题意将输出分为四个类别&#xff0c;即德才均达标&#xff0c;德达标&#xff0c;德大于才&#xff0c;其余。先按照类比来比较&#xff0c;类别相同&#xff0c;比较总分&#xff0c;总分相同比较德分…

A1042

Shuffling Machine (20分) 单词&#xff1a; shuffling&#xff1a;洗牌 position&#xff1a;位置the number at the i-th position is j, it means to move the card from position i to position j&#xff1a;第i个位置上是j&#xff0c;表示移动第i个位置上的数…