The key of this problem is that we need not build the tree from scratch. In fact, we can direct obtain its post-order traversal results in a recursive manner and the problem has given nice hints on this.

The code is as follows.

 #include <iostream>
#include <string> using namespace std; string post_order(string pre_order, string in_order) {
if (pre_order.empty() && in_order.empty())
return "";
if (pre_order.length() == && in_order.length() == )
return pre_order;
string root = pre_order.substr(, );
int rootInOrder = ;
while (in_order[rootInOrder] != pre_order[])
rootInOrder++;
string pl = pre_order.substr(, rootInOrder);
string pr = pre_order.substr( + pl.length(), pre_order.length() - pl.length() - );
string il = in_order.substr(, rootInOrder);
string ir = in_order.substr(rootInOrder + , in_order.length() - il.length() - );
return post_order(pl, il) + post_order(pr, ir) + root;
} int main(void) {
string pre_order, in_order;
while (cin >> pre_order >> in_order)
cout << post_order(pre_order, in_order);
return ;
}

[hihoCoder] 后序遍历的更多相关文章

  1. 【hihoCoder】1049.后序遍历

    问题:http://hihocoder.com/problemset/problem/1049?sid=767510 已知一棵二叉树的前序遍历及中序遍历结果,求后序遍历结果 思路: 前序:根-左子树- ...

  2. hihocoder 1049 后序遍历

    #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常 ...

  3. 【HIHOCODER 1049】 后序遍历

    描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常有意思的玩具--一棵由小球和木棍连接起来的二叉树! 小Ho对这棵二叉树爱不释手,于是给它的每一个节 ...

  4. HihoCoder第十周:后序遍历

    也就在大二学数据结构的时候知道了树的前序遍历.后序遍历.中序遍历.之后就忘了,在之后就是大四研究生老师考我,我当时还不知道,真够丢人的.自此之后,知道了如何通过其中两个得到第三个,但是也没有编程实现过 ...

  5. hihoCoder 1049 后序遍历 最详细的解题报告

    题目来源:后序遍历 解题思路:开始时我只知道先通过先序.中序求出二叉树,然后再后序遍历二叉树,这当然也是一种解题思路,但是会做一些无用功,比如:计算二叉树.其实,可以直接通过先序序列和中序序列直接求出 ...

  6. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

  7. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  8. 剑指Offer面试题:22.二叉搜索树的后序遍历序列

    一.题目:二叉搜索树的后序遍历序列 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 例如在下面 ...

  9. codevs2010 求后序遍历

    难度等级:白银 2010 求后序遍历 题目描述 Description 输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列. 输入描述 Input Description 共两行,第一行一个字符串 ...

随机推荐

  1. Linux 压缩文件的命令行总结

    Linux压缩文件的读取 ·    *.Z       compress 程序压缩的档案: ·    *.bz2     bzip2 程序压缩的档案: ·    *.gz      gzip 程序压缩 ...

  2. FreeSWITCH小结:关于export的原理介绍及应用

    FreeSWITCH小结:关于export的原理介绍及应用 Export原理 普通export用法 在dialplan中经常会用到export,如下所示: <action application ...

  3. Atitit.跨语言异常转换机制 java c# php到js的异常转换

    Atitit.跨语言异常转换机制 java c# php到js的异常转换 1. bizEx   直接抓取,然后js catchEX1 2. Chkec runtimeEx1 3. Other异常..J ...

  4. [k8s]监控

    监控架构 参考 https://github.com/DataDog/the-monitor/blob/master/kubernetes/how-to-collect-and-graph-kuber ...

  5. c语言循环单链表

    /************************************************************************* > File Name: singleLin ...

  6. 0044 spring框架的applicationContext.xml的命名空间

    Spring框架中,创建bean,装配bean,事务控制等,可以用xml配置或者注解扫描的方法实现.如果用注解扫描,在xml配置中得加上 <context:component-scan base ...

  7. poll?transport=longpoll&connection...连接的作用

    在浏览器中打开使用VS2013开发的项目时,按F12使用浏览器调试,会发现一堆无关的请求,结构大致是:poll?transport=longpoll&connection.....一直不停的请 ...

  8. PHP学习笔记(2)字符串函数

    注意: 1.双引号中可以解析变量 2.单引号中不会解析变量

  9. spoj7001 Visible Lattice Points 莫比乌斯反演+三维空间互质对数

    /** 题目:Visible Lattice Points 链接:https://vjudge.net/contest/178455#problem/A 题意:一个n*n*n大小的三维空间.一侧为(0 ...

  10. git 工作模式

    个人在学习Git工作流的过程中,从原有的 SVN 模式很难完全理解Git的协作模式,直到有一天我看到了下面的文章,好多遗留在心中的困惑迎刃而解: 我们以使用SVN的工作流来使用Git有什么不妥? Gi ...