Problem Description
根据给定的输入序列建立一棵平衡二叉树,求出建立的平衡二叉树的树根。
Input
输入一组测试数据。数据的第1行给出一个正整数N(n <= 20),N表示输入序列的元素个数;第2行给出N个正整数,按数据给定顺序建立平衡二叉树。
Output
输出平衡二叉树的树根。
Example Input
5 88 70 61 96 120
Example Output
70
code:
#include <stdio.h>
#include <stdlib.h>
typedef struct BalanceNode
{
int data;
int d;
struct BalanceNode *lchild, *rchild;
}Balancetree;
int max(int a, int b)
{
return a>b?a:b;
}
int Deep(Balancetree *root)
{
if(!root) return -1;
else return root->d;
}
Balancetree *LL(Balancetree *root)
{
Balancetree *p = root->lchild;
root->lchild = p->rchild;
p->rchild = root;
p->d = max(Deep(p->lchild), Deep(p->rchild))+1;
root->d = max(Deep(root->lchild), Deep(root->rchild))+1;
return p;
}
Balancetree *RR(Balancetree *root)
{
Balancetree *p = root->rchild;
root->rchild = p->lchild;
p->lchild = root;
p->d = max(Deep(p->lchild), Deep(p->rchild))+1;
root->d = max(Deep(root->lchild), Deep(root->rchild))+1;
return p;
}
Balancetree *RL(Balancetree *root)
{
root->rchild = LL(root->rchild);
return RR(root);
}
Balancetree *LR(Balancetree *root)
{
root->lchild = RR(root->lchild);
return LL(root);
}
Balancetree *Creat(Balancetree *root, int x)
{
if(!root)
{
root = new BalanceNode;
root->lchild = NULL;
root->rchild = NULL;
root->data = x;
root->d = 0;
}
else if(x<root->data)
{
root->lchild = Creat(root->lchild, x);
if(Deep(root->lchild)-Deep(root->rchild)>1)
{
if(x<root->lchild->data)
{
root = LL(root);
}
else
{
root = LR(root);
}
}
}
else if(x>root->data)
{
root->rchild = Creat(root->rchild, x);
if(Deep(root->rchild)-Deep(root->lchild)>1)
{
if(x>root->rchild->data)
{
root = RR(root);
}
else
{
root = RL(root);
}
}
}
root->d = max(Deep(root->lchild), Deep(root->rchild))+1;
return root;
}
int main()
{
int n, x;
scanf("%d", &n);
Balancetree *root = NULL;
for(int i = 0;i<n;i++)
{
scanf("%d", &x);
root = Creat(root, x);
}
printf("%d\n", root->data);
return 0;
}
#include <stdlib.h>
typedef struct BalanceNode
{
int data;
int d;
struct BalanceNode *lchild, *rchild;
}Balancetree;
int max(int a, int b)
{
return a>b?a:b;
}
int Deep(Balancetree *root)
{
if(!root) return -1;
else return root->d;
}
Balancetree *LL(Balancetree *root)
{
Balancetree *p = root->lchild;
root->lchild = p->rchild;
p->rchild = root;
p->d = max(Deep(p->lchild), Deep(p->rchild))+1;
root->d = max(Deep(root->lchild), Deep(root->rchild))+1;
return p;
}
Balancetree *RR(Balancetree *root)
{
Balancetree *p = root->rchild;
root->rchild = p->lchild;
p->lchild = root;
p->d = max(Deep(p->lchild), Deep(p->rchild))+1;
root->d = max(Deep(root->lchild), Deep(root->rchild))+1;
return p;
}
Balancetree *RL(Balancetree *root)
{
root->rchild = LL(root->rchild);
return RR(root);
}
Balancetree *LR(Balancetree *root)
{
root->lchild = RR(root->lchild);
return LL(root);
}
Balancetree *Creat(Balancetree *root, int x)
{
if(!root)
{
root = new BalanceNode;
root->lchild = NULL;
root->rchild = NULL;
root->data = x;
root->d = 0;
}
else if(x<root->data)
{
root->lchild = Creat(root->lchild, x);
if(Deep(root->lchild)-Deep(root->rchild)>1)
{
if(x<root->lchild->data)
{
root = LL(root);
}
else
{
root = LR(root);
}
}
}
else if(x>root->data)
{
root->rchild = Creat(root->rchild, x);
if(Deep(root->rchild)-Deep(root->lchild)>1)
{
if(x>root->rchild->data)
{
root = RR(root);
}
else
{
root = RL(root);
}
}
}
root->d = max(Deep(root->lchild), Deep(root->rchild))+1;
return root;
}
int main()
{
int n, x;
scanf("%d", &n);
Balancetree *root = NULL;
for(int i = 0;i<n;i++)
{
scanf("%d", &x);
root = Creat(root, x);
}
printf("%d\n", root->data);
return 0;
}