SDUT OJ 3374
数据结构实验之查找二:平衡二叉树
Problem Description
根据给定的输入序列建立一棵平衡二叉树,求出建立的平衡二叉树的树根。
Input
输入一组测试数据。数据的第1行给出一个正整数N(n <= 20),N表示输入序列的元素个数;第2行给出N个正整数,按数据给定顺序建立平衡二叉树。
Output
输出平衡二叉树的树根。
Example Input
5 88 70 61 96 120
Example Output
70
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
struct node
{int h;int data;node *lchild,*rchild;
};
int depth(node *root)
{if(root==NULL)return 0;return root->h;
}
node * LL(node *p)//右转
{node *q;q=p->lchild;p->lchild=q->rchild;q->rchild=p;q->h=max(depth(q->lchild),depth(q->rchild))+1;p->h=max(depth(p->lchild),depth(p->rchild))+1;return q;
}
node * RR(node *p)//左转
{node *q;q=p->rchild;p->rchild=q->lchild;q->lchild=p;q->h=max(depth(q->lchild),depth(q->rchild))+1;p->h=max(depth(p->lchild),depth(p->rchild))+1;return q;
}
node * RL(node *p)//先右转后左转
{p->rchild= LL(p->rchild);return RR(p);
}
node * LR(node *p)//先左转后右转
{p->lchild= RR(p->lchild);return LL(p);
}
node * Insert(node * root,int k)
{if(root==NULL){root=(struct node *)malloc(sizeof(struct node));root->data=k;root->lchild=root->rchild=NULL;root->h=1;}else{if(k<root->data){root->lchild=Insert(root->lchild,k);if(depth(root->lchild)-depth(root->rchild)>1){if(root->lchild->data>k)root= LL(root);else root=LR(root);}}else{root->rchild=Insert(root->rchild,k);if(depth(root->rchild)-depth(root->lchild)>1){if(root->rchild->data<k)root= RR(root);else root=RL(root);}}}root->h=max(depth(root->lchild),depth(root->rchild))+1;return root;
}node* creat(node *root,int n)
{for(int i=0; i<n; i++){int k;scanf("%d",&k);root=Insert(root,k);}return root;
}
int main()
{int n;scanf("%d",&n);node *root;root=NULL;root=creat(root,n);printf("%d\n",root->data);
}
SDUT OJ 3374相关推荐
- 喵帕斯之副食店 (sdut oj)
** 喵帕斯之副食店 (sdut oj) ** Problem Description 莲酱又去副食店买吃的去惹.现在莲酱有一些不同面额硬币,和一个非常喜欢的糖果. 但是莲酱不知道自己能够买多少颗糖果 ...
- SDUT OJ 图练习-BFS-从起点到目标点的最短步数 (vector二维数组模拟邻接表+bfs , *【模板】 )...
图练习-BFS-从起点到目标点的最短步数 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 在古老的魔兽传说中,有两个军团,一个叫天 ...
- SDUT OJ单个字符输入和输出(顺序结构)
SDUT OnlineJudge #include<iostream> #include<string.h> using namespace std; int main() { ...
- SDUT OJ 数据结构实验之排序一:一趟快排
数据结构实验之排序一:一趟快排 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ[3109] 买买买 背包 dp
好久没做题了,今天来一发. 根据题意,可以推出状态转移方程: dp[cost][weight] = max(dp[cost][weight], dp[cost-s[i].cost][weight-s[ ...
- SDUT OJ 数据结构实验之链表四:有序链表的归并
数据结构实验之链表四:有序链表的归并 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Desc ...
- SDUT OJ -2892 A
A Time Limit: 60ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 给出n(1<= n && n <= 2*10^6) ...
- SDUT oj 选拔赛1 迷之好奇
迷之好奇 Time Limit: 2000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 FF得到了一个有n个数字的集合.不要问我为什么,有钱,任性. FF很好奇 ...
- SDUT OJ 效率至上(线段树)
效率至上 Time Limit: 5000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 题意很简单,给出一个数目为n ...
最新文章
- 初学者python编辑器-分享|Mu 入门:一个面向初学者的 Python 编辑器
- python速度比较_Python和C运算速度对比实测
- 想成为一名优秀的数据分析师,应该做些什么?
- Oracle入门(三A)之sqlplus
- 在线word转html
- PHP 设计模式之工厂模式 (静态工厂模式)
- 【ACL2020论文尝鲜】如何以低成本的数据构建高效NER模型?
- 拓端tecdat|R语言缺失值的处理:线性回归模型插补
- Julia : where与类型限定
- 全网最详细的纪录片观看&下载指南
- 文件右键点击打开方式后没有始终使用此应用打开该文件的勾选框
- 免费建站网站分享,最好的自助建站
- 看看吧,在线教育直播的这四个优势
- python方法测试生男孩女孩_用什么方法可以测出怀的是男孩女孩?还有如何才能生儿子?...
- linux vi模式下基本命令和快捷键
- 017_编写一个点名器脚本
- 用MATLAB求解非线性微分方程
- 《设计你的人生》的部分经典语录
- Python爬取“爆款剧”——《三十而已》热评,并做可视化
- matlab函数——meshgrid、mesh、surf函数