[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 共两行,第一行一个字符串 ...
随机推荐
- min-height clear
在编辑页面时,总是会遇到min-height的设置 但是设置min-height后下面的div总是 跟随min-height的高度有些漂浮,如果不想让下面的div没有漂浮的效果 可以用到样式 clea ...
- Linux常用指令总结
概述 因为平时不是经常使用Linux系统,每次用到都需要重新温习一遍,这里对自己平时经常使用到的指令做个小结,方便后面直接查阅. 常用指令 登陆root指令 sudo su - 安装软件及卸载指令 d ...
- Django中使用haystack进行全文检索时需要注意的坑
对于haystack的配置什么的我在这里就不必说什么了,毕竟一搜一大把. 直接说重点 1 当你通过继承haystack的views来自定义django 应用的views 时,你要注意heystack ...
- 弹出键盘windowsoftinputmode属性设置值
windowSoftInputMode属性设置值 2012-08-30 16:49 1592人阅读 评论(0) 收藏 举报 androidattributes活动 (1).AndroidManifes ...
- .NET面试题(二)
基础方面: 1.简单的描述一下C# 中重写,重载,隐藏的概念 C#中重写(overide)是子类继承父类后,对父类中的方法进行行为改写. 重载是指方法名相同,参数不同 重写和隐藏的定义: 重写:基类方 ...
- unity调用系统剪切板功能
package com.game.utils; import android.app.Activity; import android.content.ClipData; import android ...
- atitit.LimeSurvey 安装 attilax 总结
LimeSurvey 安装 (OSSEZ) CHS 技术参考, 2011-05-02 Author: YUCHENG HU, HA Websystems, Inc. 备忘 Copyright © HA ...
- Atitit.常用分区api的attilax总结
Atitit.常用分区api的attilax总结 1. Api 来源与oracle与mysql1 1.1. 分区定义partition by range (uid) 使用VALUES LESS TH ...
- iOS直播-播放基于RTMP协议的视频
iOS直播-播放基于RTMP协议的视频 流媒体协议介绍 1. 伪流媒体: 渐进式下载 : 边下边存, 文件会保存 使用http协议,也能够实现视频播放, 也能快进快退等, 体验上跟流媒体很像. 优酷, ...
- uva11383 Golden Tiger Claw 深入理解km算法
/** 题目: uva11383 Golden Tiger Claw 深入理解km算法 链接:https://vjudge.net/problem/UVA-11383 题意:lv 思路:lrj训练指南 ...