求后序

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;
}
根据前序遍历和中序遍历后序遍历的过程可以通过递归实现。基本思想是通过前序遍历找到根节点,然后在中序遍历中确定左右子树的范围,递归构建左右子树,并最终完成后序遍历。 ### 实现思路 1. **前序遍历的第一个元素**为当前子树的根节点。 2. 在**中序遍历中找到根节点的位置**,左边部分是左子树的中序遍历结果,右边部分是右子树的中序遍历结果。 3. 根据左子树和右子树的节点数量,从**前序遍历中分割出左子树和右子树的前序遍历结果**。 4. 递归处理左子树和右子树。 5. 最后输出根节点,完成后序遍历。 ### 示例代码 以下是一个实现该逻辑的 Python 代码示例: ```python def build_tree(preorder, inorder): if not preorder: return "" root = preorder[0] root_index = inorder.index(root) left_inorder = inorder[:root_index] right_inorder = inorder[root_index+1:] left_preorder = preorder[1:1+len(left_inorder)] right_preorder = preorder[1+len(left_inorder):] left_postorder = build_tree(left_preorder, left_inorder) right_postorder = build_tree(right_preorder, right_inorder) return left_postorder + right_postorder + root # 示例输入 preorder = "abdgcefh" inorder = "dgbaechf" # 计算后序遍历结果 postorder = build_tree(preorder, inorder) print("后序遍历结果:", postorder) ``` ### 示例分析 以 `preorder = "abdgcefh"` 和 `inorder = "dgbaechf"` 为例: 1. 前序遍历的第一个字符 `a` 是根节点。 2. 中序遍历中 `a` 的位置为索引 3,左边部分 `"dgb"` 是左子树的中序遍历,右边部分 `"echf"` 是右子树的中序遍历。 3. 根据左子树节点数量(3 个),从前序遍历中提取左子树的前序遍历 `"bdg"` 和右子树的前序遍历 `"cefh"`。 4. 递归处理左子树和右子树,最终合并结果得到后序遍历 `"gdbehfca"` [^2]。 ### 复杂度分析 - **时间复杂度**:$O(n^2)$,其中 $n$ 是节点数量。每次递归需要线性时间查找根节点在中序遍历中的位置。 - **空间复杂度**:$O(n)$,用于递归调用栈和存储中间结果 [^3]。 ### 相关问题 1. 如何根据后序遍历和中序遍历前序遍历? 2. 如何通过递归和非递归方法实现前序、中序、后序遍历? 3. 如何根据二叉树的遍历结果重建二叉树? 4. 如何判断某个遍历序列是否是合法的二叉树遍历结果? 5. 如何优化根据前序和中序遍历构建二叉树的算法性能?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值