SDUT 3374 数据结构实验之查找二:平衡二叉树
数据结构实验之查找二:平衡二叉树
Problem Description
根据给定的输入序列建立一棵平衡二叉树,求出建立的平衡二叉树的树根。
Input
输入一组测试数据。数据的第1行给出一个正整数N(n <= 20),N表示输入序列的元素个数;第2行给出N个正整数,按数据给定顺序建立平衡二叉树。
Output
输出平衡二叉树的树根。
Example Input
5
88 70 61 96 120
Example Output
70
DQE:
#include <iostream>
#include <cstdio>
using namespace std;
/*
全测数据
in
9
16 3 7 11 9 26 18 14 15
out
11 7 3 9 18 15 14 16 26
3 7 9 11 14 15 16 18 26
*/
struct BT
{
int d;
int x;
BT *lt,*rt;
}; int dp(BT *r)
{
return r?r->d:;
}
void rfd(BT *r)
{
int ld=dp(r->lt)+,rd=dp(r->rt)+;
r->d=ld>rd?ld:rd;
} void LL(BT *&r)
{
// printf("LL#%d#%d*%d ",r->x,dp(r->lt),dp(r->rt));
BT *a=r,*b=a->lt,*br=b->rt;
r=b; //利用引用修改上级指向
b->rt=a;
a->lt=br;
//深度重定
rfd(a);
}
void RR(BT *&r)
{
// printf("RR#%d#%d*%d ",r->x,dp(r->rt),dp(r->lt));
BT *a=r,*b=a->rt,*bl=b->lt;
r=b;
b->lt=a;
a->rt=bl; rfd(a);
}
void LR(BT *&r)
{
RR(r->lt);
rfd(r->lt);
LL(r);
}
void RL(BT *&r)
{
LL(r->rt);
rfd(r->rt);
RR(r);
} void insert(BT *&root,int e)
{
if(!root)
{
BT *r=new BT;
r->x=e;
r->d=;
r->lt=r->rt=NULL;
root=r;
}
else
{
if(e<root->x)
{
insert(root->lt,e);
if(dp(root->lt)-dp(root->rt)>)
{
if(e<root->lt->x)
LL(root);
else
LR(root);
}
}
else if(e>root->x)
{
insert(root->rt,e);
if(dp(root->rt)-dp(root->lt)>)
{
if(e>root->rt->x)
RR(root);
else
RL(root);
}
}
//更新根深度
rfd(root);
}
} void xout(BT *r)
{if(r){printf("%d ",r->x);xout(r->lt);xout(r->rt);}}
void mout(BT *r)
{if(r){mout(r->lt);printf("%d ",r->x);mout(r->rt);}}
int main()
{
BT *root=NULL;
int n,i;
scanf("%d",&n);
while(n--)
{
scanf("%d",&i);
insert(root,i);
}
/*
putchar('\n');
xout(root);
putchar('\n');
mout(root);
*/
printf("%d\n",root->x);
return ;
} /***************************************************
User name: ***
Result: Accepted
Take time: 0ms
Take Memory: 156KB
Submit time: 2016-11-29 19:09:25
****************************************************/
SDUT 3374 数据结构实验之查找二:平衡二叉树的更多相关文章
- SDUTOJ 3374 数据结构实验之查找二:平衡二叉树
题目链接:http://acm.sdut.edu.cn/onlinejudge2/index.php/Home/Index/problemdetail/pid/3374.html 题目大意 略. 分析 ...
- SDUT 3376 数据结构实验之查找四:二分查找
数据结构实验之查找四:二分查找 Time Limit: 20MS Memory Limit: 65536KB Submit Statistic Problem Description 在一个给定的无重 ...
- SDUT OJ 数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT OJ 数据结构实验之串二:字符串匹配
数据结构实验之串二:字符串匹配 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descrip ...
- SDUT OJ 数据结构实验之链表二:逆序建立链表
数据结构实验之链表二:逆序建立链表 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descr ...
- SDUT 3399 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 冒泡排序和快 ...
- SDUT 3379 数据结构实验之查找七:线性之哈希表
数据结构实验之查找七:线性之哈希表 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 根据给定 ...
- SDUT 3377 数据结构实验之查找五:平方之哈希表
数据结构实验之查找五:平方之哈希表 Time Limit: 400MS Memory Limit: 65536KB Submit Statistic Problem Description 给定的一组 ...
随机推荐
- vector map迭代器失效解决方案
vector : iter = container.erase(iter); //erase的返回值是删除元素下一个元素的迭代器 vector<int>::iterator it = ...
- C程序设计语言阅读笔记
预处理器 ->.i 编译器 >.s 汇编器 >.o 链接器 --可执行文件 ------------------ math.h头文件包含各种数学函数的声明,所有函数都返回一个 ...
- OpenCV - Android Studio 2.2 中利用CAMKE进行OpenCV的NDK开发
我在http://www.cnblogs.com/fx-blog/p/8206737.html一文中提到了如何在Android Studio中Java层导入OpenCV(包含opencv_contri ...
- 删除文件夹里的 .svn,
删除文件夹里的 .svn,cmd 进入相应目录 运行 for /r ./ %a in (./) do @if exist "%a/.svn" rd /s /q " ...
- memcached数据库
Python-memcached的基本使用
- POJ - 3150 :Cellular Automaton(特殊的矩阵,降维优化)
A cellular automaton is a collection of cells on a grid of specified shape that evolves through a nu ...
- The Great Mixing
Sasha and Kolya decided to get drunk with Coke, again. This time they have k types of Coke. i-th typ ...
- Network Saboteur (深搜递归思想的特殊使用)
个人心得:对于深搜的使用还是不到位,对于递归的含义还是不太清楚!本来想着用深搜构成一个排列,然后从一到n分割成俩个数组,然后后面发现根本实现不了,思路太混乱.后来借鉴了网上的思想,发现用数组来标志,当 ...
- 洛谷 4245 【模板】任意模数NTT——三模数NTT / 拆系数FFT
题目:https://www.luogu.org/problemnew/show/P4245 三模数NTT: 大概是用3个模数分别做一遍,用中国剩余定理合并. 前两个合并起来变成一个 long lon ...
- nignx 重启
sudo /opt/nginx/sbin/nginx -s stop sudo /opt/nginx/sbin/nginx