求后序

A binary tree is a finite set of vertices that is either empty or consists of a root r and two disjoint binary trees called the left and right subtrees. There are three most important ways in which the vertices of a binary tree can be systematically traversed or ordered. They are preorder, inorder and postorder. Let T be a binary tree with root r and subtrees T1,T2.

In a preorder traversal of the vertices of T, we visit the root r followed by visiting the vertices of T1 in preorder, then the vertices of T2 in preorder.

In an inorder traversal of the vertices of T, we visit the vertices of T1 in inorder, then the root r, followed by the vertices of T2 in inorder.

In a postorder traversal of the vertices of T, we visit the vertices of T1 in postorder, then the vertices of T2 in postorder and finally we visit r.

Now you are given the preorder sequence and inorder sequence of a certain binary tree. Try to find out its postorder sequence.
在这里插入图片描述

Input
The input contains several test cases. The first line of each test case contains a single integer n (1<=n<=1000), the number of vertices of the binary tree. Followed by two lines, respectively indicating the preorder sequence and inorder sequence. You can assume they are always correspond to a exclusive binary tree.
Output
For each test case print a single line specifying the corresponding postorder sequence.
Sample Input
9
1 2 4 7 3 5 8 9 6
4 7 2 1 8 5 9 3 6
Sample Output
7 4 2 8 9 5 6 3 1

题意:给出一棵二叉树的前序和中序,输出后序

思路:和求前序的思路差不多,利用每棵树的前序第一个为根节点,找到中序中与之对应的值,把中序分为左树和右树,把根节点保存(保存是在两个递归调用之后的)。

代码:

#include"stdio.h"
#include"string.h"
char a[1010],b[1010],s[1010];
int book[1010],k;
void dfs(int l1,int r1,int l2,int r2)
{
    int root,left,right;
    root=book[int(a[l1]-'A'+1)];
    left=root-l2;
    right=r2-root;
    if(left)
    dfs(l1+1,l1+root-1,l2,root-1);//左树
    if(right)
    dfs(l1+left+1,r1,root+1,r2);//右树
    s[k++]=a[l1];
}
int main()
{
    while(~scanf("%s %s",a,b))
    {
        int i,l;
        k=0;
        l=strlen(a);
        for(i=0;i<l;i++)
        book[int(b[i]-'A'+1)]=i;
        dfs(0,l-1,0,l-1);
        for(i=0;i<l;i++)
		printf("%c",s[i]);
        printf("\n");
    }
    return 0;
}
在C语言中,给定一棵二叉树的前序遍历和中序遍历,你可以通过递归或者栈来得它的后序遍历。这是因为前序、中序和后序遍历分别对应着根节点、左子树和右子树的信息。 以下是一个基本的算法步骤: 1. **前序遍历** (Preorder) - 访问根节点 -> 左子树 -> 右子树 2. **中序遍历** (Inorder) - 左子树 -> 根节点 -> 右子树 **后序遍历** (Postorder): - 我们知道中序遍历已经按照"左->根->右"的顺序访问了所有节点。所以,如果我们在中序遍历中找到根节点的位置,它之前的部分就是左子树,之后的部分就是右子树。 - 首先我们需要找出根节点的索引(假设数组下标从0开始),这可以通过前序遍历找到,因为前序的第一个元素就是根节点。 - 接着,我们可以在中序遍历中,从根节点的下一个位置开始,取到的序列就是后序遍历的结果。 下面是伪代码示例: ```c int* preOrder, *inOrder, size; // 假设这两个数组存储了前序和中序遍历结果 int rootIndex = -1; // 存储前序遍历中的根节点索引 // 找到根节点索引 for (size_t i = 0; i < size; i++) { if (preOrder[i] == inOrder[size - 1]) { rootIndex = i; break; } } // 后序遍历数组 int* postOrder = new int[size]; // 新建后序遍历结果数组 postOrder[0] = inOrder[rootIndex]; // 将根节点放在第一个位置 size--; // 剩余部分:将剩余的中序部分分左右两部分,分别复制到后序数组 int leftSize = findLeftSubtree(inOrder + rootIndex + 1, inOrder + size); copyFromInOrder(postOrder + 1, inOrder + rootIndex + 1, leftSize); copyFromInOrder(postOrder + 1 + leftSize, inOrder + rootIndex + 1 + leftSize, size - leftSize); delete[] postOrder; // 注意释放内存 // 辅助函数:找到某个节点的左子树大小 int findLeftSubtree(int start, int end) { for (int i = start; i < end; i++) { if (preOrder[rootIndex] != inOrder[i]) { return i - start; } } return end - start; } // 辅助函数:从源数组复制数据到目标数组 void copyFromInOrder(int* dest, int* src, int size) { for (int i = 0; i < size; i++, src++, dest++) { *dest = *src; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值