1119. Pre- and Post-order Traversals (30)

本文介绍如何通过前序和后序遍历序列来判断能否唯一确定一颗二叉树的方法,并提供了一段C++代码实现。文章指出,在特定条件下(即某个节点只有单一子树时),前序和后序遍历无法唯一确定二叉树,通过检查这些条件,可以判断给定的序列是否对应唯一的二叉树。

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

1119. Pre- and Post-order Traversals (30)
对于前序和中序,或者后序和中序,可以唯一确定一棵二叉树,而对于前序和后序,可能不能唯一确定一棵二叉树,原因在于,前序和后序对于左右子树的划分可能是不确定的。并且这种不确定,只可能出现在具有单一子树的节点中,这是因为,建树过程中,将该子树放在左边,或者右边,都是可以的,因为这两种方式都将产生相同的前序和后序序列。因此,判断其是否unique便依据于此:倘若在一次递归建树中,位于前序序列的第二个元素(是第一个元素的子树根节点,左儿子或者右儿子),和后序序列中倒数第二个元素相同,这意味者,对于根节点来说(前序第一个元素),其只有一个儿子(要么是左儿子,要么是右儿子),这将不是unique的。

#include <bits/stdc++.h>
using namespace std;
int n,pre[32],pos[32];
bool isunique=1;
vector<int> ino;
void build(int preL,int preR,int posL,int posR)
{
    if(preL>preR) return;
    if(preL==preR){
        ino.push_back(pre[preL]);
        return;
    }
    int e=pre[preL+1],prex=preL+1,posx=posL;
    while(posx<posR&&e!=pos[posx]) ++prex,++posx;
    if(posx==posR-1) isunique=0;
    build(preL+1,prex,posL,posx);
    ino.push_back(pre[preL]);
    build(prex+1,preR,posx+1,posR-1);
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%d",&pre[i]);
    }
    for(int i=0;i<n;++i){
        scanf("%d",&pos[i]);
    }
    build(0,n-1,0,n-1);
    isunique?printf("Yes\n"):printf("No\n");
    for(int i=0;i<n;++i){
        i==n-1?printf("%d\n",ino[i]):printf("%d ",ino[i]);
    }
    return 0;
}
American Heritage 题目描述 Farmer John takes the heritage of his cows very seriously. He is not, however, a truly fine bookkeeper. He keeps his cow genealogies as binary trees and, instead of writing them in graphic form, he records them in the more linear tree in-order&quot; and tree pre-order&quot; notations. Your job is to create the `tree post-order&quot; notation of a cow&quot;s heritage after being given the in-order and pre-order notations. Each cow name is encoded as a unique letter. (You may already know that you can frequently reconstruct a tree from any two of the ordered traversals.) Obviously, the trees will have no more than 26 nodes. Here is a graphical representation of the tree used in the sample input and output: C / \ / \ B G / \ / A D H / \ E F The in-order traversal of this tree prints the left sub-tree, the root, and the right sub-tree. The pre-order traversal of this tree prints the root, the left sub-tree, and the right sub-tree. The post-order traversal of this tree print the left sub-tree, the right sub-tree, and the root. ---------------------------------------------------------------------------------------------------------------------------- 题目大意: 给出一棵二叉树的中序遍历 (inorder) 和前序遍历 (preorder),求它的后序遍历 (postorder)。 输入描述 Line 1: The in-order representation of a tree. Line 2: The pre-o rder representation of that same tree. Only uppercase letter A-Z will appear in the input. You will get at least 1 and at most 26 nodes in the tree. 输出描述 A single line with the post-order representation of the tree. 样例输入 Copy to Clipboard ABEDFCHG CBADEFGH 样例输出 Copy to Clipboard AEFDBHGC c语言,代码不要有注释
06-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值