还原二叉树 (C语言)

题目描述

给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。

输入描述

输入首先给出正整数N(≤50),为树中结点总数。下面两行先后给出先序和中序遍历序列,均是长度为N的不包含重复英文字母(区别大小写)的字符串。

输出描述

输出为一个整数,即该二叉树的高度。

输入样例
9
ABDFGHIEC
FDHGIBEAC

输出样例
5


//这题就是直接用之前通过中序和前序找后序的代码直接改的,就是把输出后序改成了计算高度
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

typedef struct Node
{
    char data;
    struct Node *lchild;
    struct Node *rchild;
}List;

int h, max;
char pre[30], in[30];

//当前先序序列的区间为[preL, preR], 中序序列区间为[inL, inR],返回根节点的地址
List* Create(int preL, int preR, int inL, int inR)
{
    if(preL > preR)
    {
        return NULL; //先序序列长度小于等于0时,直接返回
    }
    List *root = (List *)malloc(sizeof(List));
    root->data = pre[preL];
    int k;
    for(k = inL; k <= inR; k++)
    {
        if(in[k] == pre[preL]) //在中序序列中找到in[k] == pre[preL]的结点
        {
            break;
        }
    }
    int numLeft = k - inL; //对于当前结点的左子树的结点的个数
    root->lchild = Create(preL+1, preL+numLeft, inL, k-1);
    root->rchild = Create(preL+numLeft+1, preR, k+1, inR);

    return root; //返回根结点地址
}

void Postorder(List *root, int k)
{
    if(root != NULL)
    {
        if(k > max)
            max = k;
       Postorder(root->lchild, k+1);
       Postorder(root->rchild, k+1);
    }


}

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        scanf("%s", pre);
        scanf("%s", in);
        h = strlen(pre);
        max = 0;
        List *root = Create(0, h-1, 0, h-1); //建树
        Postorder(root, 1);
        printf("%d\n", max);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42819248/article/details/84143252
今日推荐