Description
根据给定的输入序列建立一棵平衡二叉树,求出建立的平衡二叉树的树根。
Input
输入一组测试数据。数据的第1行给出一个正整数N(n <= 20),N表示输入序列的元素个数;第2行给出N个正整数,按数据给定顺序建立平衡二叉树。
Output
输出平衡二叉树的树根。
Sample
Input
5
88 70 61 96 120
Output
70
理解不上可以画图试试
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int data,deep;
node *l,*r;
}Tree;
//求深度
int Deep(Tree *root)
{
if(!root)
return -1;
else
return root->deep;
}
//返回新的深度值
int New_Deep(Tree *p)
{
return max(Deep(p->l),Deep(p->r))+1;
}
//当深度差大于1发生在左子树的左孩子
//单向右旋
Tree* LL