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. Vue基础及脚手架环境搭建

    From:http://www.jianshu.com/p/dc5057e7ad0d 一.vue基础 “Vue2.0”跟俺一起全面入坑 01 “Vue2.0”跟俺一起全面入坑 02 “Vue2.0”跟 ...

  2. Node.js搭建本地web服务(转)

    http://www.cnblogs.com/wangfupeng1988/p/4143996.html https://github.com/finderL/webserver

  3. hsqldb

    http://www.hsqldb.org/ HSQLDB (HyperSQL DataBase) is the leading SQL relational database software wr ...

  4. Android Service演义

    摘要: 本文基于Android 5.1代码,介绍了Android Service的运作机理.按理说,网上此类文章已经很多了,本不需我再赘述.但每个人理解技术的方式多少会有所不同,我多写一篇自己理解的s ...

  5. ssh远程主机的免密登录配置

    ssh经常登录远程主机,需要输入密码,很麻烦,怎样变懒呢? test environment:      hostA:ubuntu14.04  username:frank ip:192.168.1. ...

  6. linux学习笔记24---命令grep

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是global search regular expression(RE) and ...

  7. 如何在线制作gif图片?

    最近想做个gif在线制作的网站,所以研究下了imagemagick和graphicsmagick制作gif图片站已经做出来了:有兴趣的朋友可以先看看http://www.sosogif.com/mak ...

  8. 原生javascript 实现的文本编辑器

    直接来干活上代码.就一个函数execCommand():自己百度一下. 在线预览 <!doctype html> <html> <head> <title&g ...

  9. java请求POST发送json格式请求

    public static String upload(String url){ try { HttpClient httpclient = new DefaultHttpClient(); Http ...

  10. ThinkPHP 模板 Volist 标签嵌套循环输出多维数组

    ThinkPHP 中对 volist 标签嵌套使用可实现多维数组的输出. volist 嵌套使用 一般的二维数组,可以用 volist 标签直接循环输出.对于多维数组,则需要对其中的数组成员再次使用 ...