思路:中序遍历–根结点,左子树,右子树;后序遍历–左子树,右子树,根结点。

那么在找到根结点之后就可以开始划分左右子树了。左子树的先序第一个节点是根,左子树的后序最后一个节点是根。

例如

1 2 3 4 6 7 5

2 6 7 4 5 3 1

当前的根结点就是1,在先序中得到左子树的根结点就是2,再在后序中知道左子树的节点就是{2},那么就得到左子树了,剩下的就是右子树。

如何判断重建的树是否唯一?非叶结点如果只有左子树或者右子树,那么就不唯一。

注意:如果你使用数组实现的二叉树,那么把数组开大一点,只开maxn=30+5的话第一组数据会WA(本人WA了半个小时才猜到这个套路),后来直接开10000过掉


AC代码

#include <stdio.h>
#include <string.h>
const int maxn = 10000+5;
bool isUnique;
int pre[maxn], post[maxn];
int left[maxn], right[maxn];
int ans[maxn], nodes;

//return root
int reBuild(int prel, int prer, int postl, int postr) {
    if(prel > prer) return -1;
    int root = pre[prel];
    if(prel == prer) { //leaf
        return root;
    }
    //left-tree
    int l1 = prel+1, r2;
    for(r2 = postl; r2 < postr; r2++) {
        if(post[r2] == pre[l1]) {
            break;
        }
    }
    int len = r2 - postl + 1;
    // check unique
    if(len == prer-prel) {
        isUnique = false;
    }
    int r1 = l1 + len -1;
    int l2 = postl;
    //rebulid left-tree
    left[root] = reBuild(l1, r1, l2, r2);
    //rebuild right-tree
    right[root] = reBuild(r1+1, prer, r2+1, prer-1);
    return root;
}

void getInorder(int root) {
    if(root == -1) return;
    getInorder(left[root]);
    ans[nodes++] = root;
    getInorder(right[root]);
}

int main() {
    int n;
    while(scanf("%d", &n) == 1) {
        isUnique = true;
        memset(left, -1, sizeof(left));
        memset(right, -1, sizeof(right));
        for(int i = 0; i < n; i++) {
            scanf("%d", &pre[i]);
        }
        for(int i = 0; i < n; i++) {
            scanf("%d", &post[i]);
        }
        int root = reBuild(0, n-1, 0, n-1);
        nodes = 0;
        getInorder(root);
        if(isUnique) {
            printf("Yes\n");
        } else {
            printf("No\n");
        }
        for(int i = 0; i < nodes; i++) {
            printf("%d%c", ans[i], i == nodes-1 ? '\n' : ' ');
        }
    }
    return 0;
}

如有不当之处欢迎指出!

PAT1119. Pre- and Post-order Traversals的更多相关文章

  1. Construct a tree from Inorder and Level order traversals

    Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...

  2. [LeetCode] Rank Scores 分数排行

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  3. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  4. ASP.NET MVC : Action过滤器(Filtering)

    http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...

  5. HDU 1160 FatMouse's Speed

    半个下午,总算A过去了 毕竟水题 好歹是自己独立思考,debug,然后2A过的 我为人人的dp算法 题意: 为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点 本着“指 ...

  6. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  7. Spring Cloud Zuul 限流详解(附源码)(转)

    在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选择,只需要编写一个过滤器就可以了,关键在于如何实现限流的算法. ...

  8. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  9. LeetCode: Recover Binary Search Tree 解题报告

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  10. [LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal

    Pre: node 先,                      Inorder:   node in,           Postorder:   node 最后 PreOrder Inorde ...

随机推荐

  1. linux_用户和组

    linux用户分为3类: 超级用户:root, UID为0, GID为0 普通用户: 500 -65535, 由root创建 虚拟用户: 1-499 - 系统里傀儡,不能使用,固定存在,满足linux ...

  2. 转-Windows路由表配置:双网卡路由分流

    原文链接:http://www.cnblogs.com/lightnear/archive/2013/02/03/2890835.html 一.windows 路由表解释 route print -4 ...

  3. Linux修改主机名脚本-不重启-支持RedHat、SUSE

    需要用脚本修改主机名,涉及RedHat.SUSE系统,并且要求修改立即生效且不重启,下面就是我的脚本. 使用脚本的方法如下: 1 首先创建一个脚本文件,假如命名为ModifyHostname.sh: ...

  4. 无废话XML--XML约束(DTD)

    基本术语     一.序言Prolog:包括XML声明(XML Declaration)和文档类型声明(Document Type Declaration).     二.良构(well-formed ...

  5. Oracle Start With关键字

    Oracle Start With关键字 前言 旨在记录一些Oracle使用中遇到的各种各样的问题. 同时希望能帮到和我遇到同样问题的人. Start With (树查询) 问题描述: 在数据库中, ...

  6. Java Cookie和Session

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  7. HTTP就是这么简单

    为什么要学HTTP? 我们绝大多数的Web应用都是基于HTTP来进行开发的.我们对Web的操作都是通过HTTP协议来进行传输数据的. 简单来说,HTTP协议就是客户端和服务器交互的一种通迅的格式. H ...

  8. 我是这么配置mariadb的。 为了能够操作汉字数据~

    为了能够操作汉字数据- 以下是步骤: 1. 找到my.cnf /etc/my.cnf 2. 打开它,在[client]和[mysql]下输入以下指令 default-character-set=utf ...

  9. BZOJ 4269: 再见Xor [高斯消元 线性基]

    4269: 再见Xor Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. 我太愚蠢了连数组开小了以及$2^{ ...

  10. transform复习之图片的旋转木马效果

    效果示意图 <!DOCTYPE><html><head><meta http-equiv="Content-Type" content=& ...