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. Jekins部署.net站点

    前提 1.你需要一台windows服务 可以装vs的且有重启电脑权限的(具体vs版本根据你的团队决定) 2.下载jekins 安装包 地址:https://jenkins.io/download/   ...

  2. php 删除文件夹下的所有文件

    $patch = dirname(__FILE__).'/Cookie/';//获取文件目录 $files = scandir($patch); foreach ($files as $filenam ...

  3. UIView的使用setNeedsDisplay

    1,UIView的setNeedsDisplay和setNeedsLayout方法   首先两个方法都是异步运行的.而setNeedsDisplay会调用自己主动调用drawRect方法.这样能够拿到 ...

  4. Android Studio编译的时候提示Gradle无法下载的解决方案

    首先,打开android studio项目 找到项目目录gradle\wrapper\gradle-wrapper.properties这个文件.内容如下:#Wed Apr 10 15:27:10 P ...

  5. matplotlib极坐标系应用之雷达图

    #!/usr/bin/env python3 #-*- coding:utf-8 -*- ############################ #File Name: test.py #Autho ...

  6. sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛

    Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...

  7. HDOJ 4884 & BestCoder#2 1002

    TIANKENG’s rice shop Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  8. jsp tld 的tag 自定义标签扩展

    引入方式 <%@ taglib prefix="bgt" uri="/WEB-INF/tlds/bgt.tld" %> 写法示例如下: <?x ...

  9. MVVM 实战之计算器

    MVVM 实战之计算器 android DataBinding MVVM calculator Model View 布局文件 Fragment ViewModel 结束语 前些日子,一直在学习基于 ...

  10. 删除Win10的OneDrive

    1.运行 -> gpedit.msc 计算机配置 -> 管理模板 -> Windows组件 -> OneDrive -> 禁止使用OneDrive进行文件储存 -> ...