由两种遍历序列确定二叉树

已知中序和后序遍历

typedef int ElemType;
typedef struct TNode *Position;
typedef Position BinTree;
typedef struct TNode
{
   ElemType data;
   BinTree lchild, rchild;
};
BinTree creat(int *a,int *b,int n)	//数组a,b分别存放中序,后序遍历结果
{
    BinTree BT;
    if(n<=0)  return NULL;
    BT = (BinTree)malloc(sizeof(struct TNode));
    BT->data = b[n-1];
    int q;
    for(int i=0;i<n;i++){
        if(a[i]==b[n-1]){
            q = i;
            break;
        }
    }
    BT->lchild = creat(a,b,q);
    BT->rchild = creat(a+q+1,b+q,n-q-1);
    return BT;
}

7-35 根据后序和中序遍历输出先序遍历 (25 分)
本题要求根据给定的一棵二叉树的后序遍历和中序遍历结果,输出该树的先序遍历结果。

输入格式:
第一行给出正整数N(≤30),是树中结点的个数。随后两行,每行给出N个整数,分别对应后序遍历和中序遍历结果,数字间以空格分隔。题目保证输入正确对应一棵二叉树。

输出格式:
在一行中输出Preorder:以及该树的先序遍历结果。数字间有1个空格,行末不得有多余空格。

输入样例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
输出样例:
Preorder: 4 1 3 2 6 5 7

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int maxn = 100;
typedef int ElemType;
typedef struct TNode *Position;
typedef Position BinTree;
typedef struct TNode
{
   ElemType data;
   BinTree lchild, rchild;
};
int p[maxn],q[maxn];
BinTree creat(int *a,int *b,int n)
{
    BinTree BT;
    if(n<=0)  return NULL;
    BT = (BinTree)malloc(sizeof(struct TNode));
    BT->data = b[n-1];
    int q;
    for(int i=0;i<n;i++){
        if(a[i]==b[n-1]){
            q = i;
            break;
        }
    }
    BT->lchild = creat(a,b,q);
    BT->rchild = creat(a+q+1,b+q,n-q-1);
    return BT;
}
void Preorder(BinTree T)
{
    if(T){
        printf(" %d",T->data);
        Preorder(T->lchild);
        Preorder(T->rchild);
    }
}
int main(void)
{
    int n,x;

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&q[i]);
    for(int i=0;i<n;i++)
        scanf("%d",&p[i]);

    BinTree T = creat(p,q,n);
    printf("Preorder:");
    Preorder(T);
    return 0;
}

已知 前序和中序遍历

typedef char ElemType;
typedef struct TNode *Position;
typedef Position BinTree;
typedef struct TNode
{
   ElemType data;
   BinTree lchild, rchild;
};
BinTree creat(char *a,char *b,int n)
{
    BinTree BT;
    if(n<=0)  return NULL;
    BT = (BinTree)malloc(sizeof(struct TNode));
    BT->data = *a;
    int q;
    for(int i=0;i<n;i++){
        if(*a==b[i]){
            q = i;
            break;
        }
    }
    BT->lchild = creat(a+1,b,q);
    BT->rchild = creat(a+q+1,b+q+1,n-q-1);
    return BT;
}

7-36 还原二叉树 (25 分)
给定一棵二叉树的先序遍历序列和中序遍历序列,要求计算该二叉树的高度。

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

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

输入样例:
9
ABDFGHIEC
FDHGIBEAC
输出样例:
5

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int maxn = 100;
typedef char ElemType;
typedef struct TNode *Position;
typedef Position BinTree;
typedef struct TNode
{
   ElemType data;
   BinTree lchild, rchild;
};
char p[maxn],q[maxn];
BinTree creat(char *a,char *b,int n)
{
    BinTree BT;
    if(n<=0)  return NULL;
    BT = (BinTree)malloc(sizeof(struct TNode));
    BT->data = *a;
    int q;
    for(int i=0;i<n;i++){
        if(*a==b[i]){
            q = i;
            break;
        }
    }
    BT->lchild = creat(a+1,b,q);
    BT->rchild = creat(a+q+1,b+q+1,n-q-1);
    return BT;
}
int GetHeight(BinTree T)
{
    int hl,hr,hm;
    if(T){
        hl = GetHeight(T->lchild);
        hr = GetHeight(T->rchild);
        hm = hl > hr ? hl : hr;
        return(hm+1);
    }
    else    return 0;
}
int main(void)
{
    int n,x;

    scanf("%d",&n);
    scanf("%s",p);
    scanf("%s",q);

    BinTree T = creat(p,q,n);
    printf("%d\n",GetHeight(T));
    return 0;
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逃夭丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值