已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历和层序遍历

本文介绍了一种根据给定的二叉树前序遍历和中序遍历序列,求解该二叉树的后序遍历和层次遍历的方法。通过递归构建二叉树,并实现后序遍历及层次遍历的输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据结构实验之求二叉树后序遍历和层次遍历

Time Limit: 1000 ms Memory Limit: 65536 KiB

Submit Statistic

Problem Description

 已知一棵二叉树的前序遍历和中序遍历,求二叉树的后序遍历和层序遍历。

Input

 输入数据有多组,第一行是一个整数t (t<1000),代表有t组测试数据。每组包括两个长度小于50 的字符串,第一个字符串表示二叉树的先序遍历序列,第二个字符串表示二叉树的中序遍历序列。

Output

每组第一行输出二叉树的后序遍历序列,第二行输出二叉树的层次遍历序列。

Sample Input

2
abdegcf
dbgeafc
xnliu
lnixu

Sample Output

dgebfca
abcdefg
linux
xnuli
#include <stdio.h>
#include <stdlib.h>
char s[1500];
int i;
typedef struct
{
    char data;
    struct tree *Lchild;
    struct tree *Rchild;
}tree;

struct tree * pai(char *xian, char *zhong, int len)
{
    if(len == 0)
        return NULL;
    tree *temp = (tree *)malloc(sizeof(tree));
    temp -> data = *xian;
    int i;
    for(i=0;i<len;i++)
    {
        if(zhong[i]==*xian)
        {
            break;
        }
    }
    temp -> Lchild = pai(xian+1, zhong, i);
    temp -> Rchild = pai(xian+i+1, zhong+i+1,len-i-1);
    return temp;
};




levelshow(tree *head)
{
    tree *temp[100];
    int front=0, rear=-1;
    temp[++rear] = head;
    while(rear>=front)
    {
        if(temp[front])
        {
        printf("%c", temp[front]->data);
        temp[++rear] = temp[front]->Lchild;
        temp[++rear] = temp[front]->Rchild;
        }
        front++;
    }

}

postshow(tree *temp)
{
    if(temp)
    {
        postshow(temp->Lchild);
        postshow(temp->Rchild);
        printf("%c", temp->data);
    }
}

int main()
{
    char zhong[100], xian[100];
    int i, len;
    tree *head;
    int n;
    scanf("%d", &n);
    while(n--)
    {
    scanf("%s", xian);
    scanf("%s", zhong);
    len = strlen(zhong);
    head = pai(xian, zhong, len);//建树
    postshow(head);
    printf("\n");
    levelshow(head);//层次遍历
    printf("\n");
    }
    return 0;
}

 

根据二叉树前序遍历序遍历可以唯一确定一棵二叉树,因此可以通过这两个遍历序列构建出这棵二叉树,然后再进行后序遍历。 具体步骤如下: 1. 根据前序遍历序列确定根节点,假设为root。 2. 在中序遍历序列中找到根节点root的位置,将中序遍历序列分为左子树右子树两部分,分别对左右子树递归进行步骤1步骤2,直到序列为空或者只有一个节点。 3. 对于每个节点,先遍历它的左子树,再遍历它的右子树,最后遍历它本身,即可得到后序遍历序列。 下面是Python代码实现: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def buildTree(preorder, inorder): if not preorder or not inorder: return None root_val = preorder[0] root = TreeNode(root_val) root_index = inorder.index(root_val) root.left = buildTree(preorder[1:root_index+1], inorder[:root_index]) root.right = buildTree(preorder[root_index+1:], inorder[root_index+1:]) return root def postorderTraversal(root): if not root: return [] stack = [root] res = [] while stack: node = stack.pop() res.append(node.val) if node.left: stack.append(node.left) if node.right: stack.append(node.right) return res[::-1] preorder = [1, 2, 4, 5, 3, 6, 7] inorder = [4, 2, 5, 1, 6, 3, 7] root = buildTree(preorder, inorder) postorder = postorderTraversal(root) print(postorder) # 输出:[4, 5, 2, 6, 7, 3, 1] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值