Description
给定一颗二叉树的先序序列,求该二叉树的高。
Input
输入包括两部分
第一部分:一个正整数n,代表有n颗二叉树
第二部分:包括n行,每行代表一颗二叉树的先序遍历序列,空指针用字符^占位
Output
n行,每行一个整数,代表对应二叉树的高度
Sample Input
2
ABC^^^^
AB^^^
Sample Output
3
2
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
char ch;
struct node *Lchild;
struct node *Rchild;
}BiTNode,*BiTree;
void Q_CreatTree(BiTree *T);
int NodeNumber_1(BiTree T);
int NodeNumber_2(BiTree T);
int NodeNumber_0(BiTree T);
void Deepth(BiTree T,int L,int *h);
int main(void)
{
int n;
scanf("%d",&n);
getchar();
while(n--)
{
int L = 0,h = 0;
BiTree T;
Q_CreatTree(&T);
getchar();
Deepth(T,L,&h);
printf("%d\n",h);
}
return 0;
}
void Q_CreatTree(BiTree *T)
{
char str;
str=getchar();
if(str=='^')
{
*T=NULL;
}
else
{
(*T)=(BiTree)malloc(sizeof(BiTNode));
(*T)->ch=str;
Q_CreatTree( &( (*T)->Lchild ) );
Q_CreatTree( &( (*T)->Rchild ) );
}
}
void Deepth(BiTree T,int L,int *h)
{
if(T)
{
L++;
if(L>*h)
*h = L;
Deepth(T->Lchild,L,h);
Deepth(T->Rchild,L,h);
}
}