前言
给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。
一、Problem Description
给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。
二、Input Description
输入首先给出正整数N(≤50),为树中结点总数。下面两行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区别大小写)的字符串
9 ABDFGHIEC FDHGIBEAC
三、Output Description
5
/*
湖 北 民 大
Create by Zhou
*/
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
typedef struct Node{
char ch;
struct Node *lc;
struct Node *rc;
};
char qianxu[50];
char zhonxu[50];
Node* qian(int b1, int e1, int b2, int e2)
{
Node* newNode = (Node *)malloc(sizeof(struct Node));
newNode->lc = newNode->rc = NULL;
newNode->ch = qianxu[b1];
int ri = -1;
for(int i = b2; i <= e2 ; i++)
{
if(qianxu[b1] == zhonxu[i])
{
ri = i;
break;
}
}
if(ri != b2) // (ri-b2) 为除根结点外的,左子树
{
newNode->lc = qian(b1 +1, b1 + (ri - b2), b2, ri - 1);
}
if(ri != e2)
{
newNode->rc = qian(b1 + (ri - b2) +1, e1, ri +1, e2);
}
return newNode;
}
int max;
void hight(Node *t, int x)
{
if(t != NULL)
{
if(x > max)
{
max = x;
}
hight(t->lc, x+1);
hight(t->rc, x+1);
}
}
int main()
{
int n;
while(scanf("%d",&n) != EOF)
{
scanf("%s",qianxu);
Node *r = NULL;
scanf("%s",zhonxu);
r = qian(0, strlen(qianxu)-1, 0, strlen(qianxu)-1);
hight(r, 1);
printf("%d",max);
break;
}
return 0;
}
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。