[hihoCoder] 后序遍历
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] 后序遍历的更多相关文章
- 【hihoCoder】1049.后序遍历
问题:http://hihocoder.com/problemset/problem/1049?sid=767510 已知一棵二叉树的前序遍历及中序遍历结果,求后序遍历结果 思路: 前序:根-左子树- ...
- hihocoder 1049 后序遍历
#1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常 ...
- 【HIHOCODER 1049】 后序遍历
描述 在参与过了美食节之后,小Hi和小Ho在别的地方又玩耍了一阵子,在这个过程中,小Ho得到了一个非常有意思的玩具--一棵由小球和木棍连接起来的二叉树! 小Ho对这棵二叉树爱不释手,于是给它的每一个节 ...
- HihoCoder第十周:后序遍历
也就在大二学数据结构的时候知道了树的前序遍历.后序遍历.中序遍历.之后就忘了,在之后就是大四研究生老师考我,我当时还不知道,真够丢人的.自此之后,知道了如何通过其中两个得到第三个,但是也没有编程实现过 ...
- hihoCoder 1049 后序遍历 最详细的解题报告
题目来源:后序遍历 解题思路:开始时我只知道先通过先序.中序求出二叉树,然后再后序遍历二叉树,这当然也是一种解题思路,但是会做一些无用功,比如:计算二叉树.其实,可以直接通过先序序列和中序序列直接求出 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [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 ...
- 剑指Offer面试题:22.二叉搜索树的后序遍历序列
一.题目:二叉搜索树的后序遍历序列 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true,否则返回false.假设输入的数组的任意两个数字都互不相同. 例如在下面 ...
- codevs2010 求后序遍历
难度等级:白银 2010 求后序遍历 题目描述 Description 输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列. 输入描述 Input Description 共两行,第一行一个字符串 ...
随机推荐
- Memcached 测试
Memcached set 命令用于将 value(数据值) 存储在指定的 key(键) 中. 如果set的key已经存在,该命令可以更新该key所对应的原来的数据,也就是实现更新的作用. 语法: s ...
- Ant—使用Ant构建简单Java项目(三)
博客<Ant-使用Ant构建简单Java项目(二)>我们简化了运行Test类中main方法须要运行的命令,本博客来介绍一下如何使build.xml文件和当中使用property标签定义的属 ...
- PHP扩展开发及内核应用(未完)
转: https://github.com/walu/phpbook
- atitit.信息系统方案规划 p71.doc
[信息系统方案规划 ] 版本 v2 2015-7-1 变更记录 日期 修改人 版本 变更事由 说明 2015.07 艾龙 1.0 初创 2015.07 艾龙 2.0 添加接口 1. 业务功能与流程设计 ...
- [svc][op]如何查看当前Ubuntu系统的版本
如何查看当前Ubuntu系统的版本 说来也惭愧,用Ubuntu差不多快1个月了,双系统是让朋友安的,只知道自己使用的是什么12版本的,具体怎么看还不知道,下面写一下查看当前Linux系统的版本的方法 ...
- urllib3学习
urllib3.connectionpool.connection_from_url(url, **kw) Given a url, return an ConnectionPool instance ...
- Windows/OS X下制作Mac安装U盘
Windows下制作: 方法一:(适用于OSX 10.9以前) 前期准备:一台windows电脑 UltraISO软件 Mac系统镜像dmg(这里使用Mac os x 10.8.4) 至少8GB的U盘 ...
- 【持续更新】总结:C++开发时积累的一些零碎的东西
Makefile template update: 1.调整了顺序,把经常编辑的部分集中在了Makefile的下半部分 2.进行了一些重构实现更多的代码复用,见红色高亮部分,LIBPATH_ALL和L ...
- iOS 给tableView设置contentInset不生效?
给tableView设置contentInset的时候如果tableView中内容比较多,超过一个屏幕,设置的contentInset是生效的,但是呢,如果页面内容比较少,我们会发现设置content ...
- ecmall 中Url体系改造实践
前面有过一篇ECMall 中URL体系的改造思路http://www.cnblogs.com/x3d/p/3627260.html 这两天基于这个思路,做了实践. 为什么要改造? ECMall是完整的 ...