poj: 2255
跟LEETCODE的preorder,inorder转postorder题很像
#include <iostream> #include <stdio.h> #include <string> #include <stack> #include <map> #include <vector> #include <algorithm> using namespace std; struct node { char val; node *left, *right; node(char v) : val(v), left(NULL), right(NULL) { } }; node* preIn(string preorder, string inorder, int prebeg, int inbeg, int len) { if (!len) return NULL; node *tmp = new node(preorder[prebeg]); int leftlen, rightlen; for (int i = inbeg; i < inbeg+len; ++i) { if (tmp->val == inorder[i]) { leftlen = i - inbeg; rightlen = len - leftlen - ; break; } } tmp->left = preIn(preorder, inorder, prebeg+, inbeg, leftlen); tmp->right = preIn(preorder, inorder, prebeg+leftlen+, inbeg+leftlen+, rightlen); return tmp; } string postString(node *root) { if (!root) return ""; return postString(root->left) + postString(root->right) + root->val; } int main() { string preorder, inorder; while (cin >> preorder && cin >> inorder) { node *root = preIn(preorder, inorder, , , preorder.size()); string postorder = postString(root); cout << postorder << endl; } ; }
poj: 2255的更多相关文章
- Tree Recovery POJ - 2255
Tree Recovery POJ - 2255 根据树的前序遍历和中序遍历还原后序遍历. (偷懒用了stl的find) #include<iostream> #include<st ...
- POJ 2255. Tree Recovery
Tree Recovery Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11939 Accepted: 7493 De ...
- POJ 2255 Tree Recovery 树的遍历,分治 难度:0
http://poj.org/problem?id=2255 #include<cstdio> #include <cstring> using namespace std; ...
- Poj 2255 Tree Recovery(二叉搜索树)
题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...
- poj 2255 Tree Recovery(求后序遍历,二叉树)
版权声明:本文为博主原创文章,未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37699219 转载请注明出处 ...
- POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)
链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...
- POJ 2255 Tree Recoveryw(二叉树)
题目原网址:http://poj.org/problem?id=2255 题目中文翻译: Description 小瓦伦丁非常喜欢玩二叉树. 她最喜欢的游戏是用大写字母构造的随机二叉树. 这是她的一个 ...
- POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)
题意:给出一颗二叉树的前序遍历和中序遍历的序列,让你输出后序遍历的序列. 思路:见代码,采用递归. #include <iostream> #include <stdio.h> ...
- POJ 2255 Tree Recovery 二叉树的遍历
前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...
随机推荐
- Bootstrap 图标菜单按钮组件
---恢复内容开始--- 一.小图标组件 Bootstrap 提供了免费的 263 个小图标(数了两次),具体可以参考中文官网的组件 链接:http://v3.bootcss.com/componen ...
- pro10
1.本次课学习到的知识点: 什么是数组?为什么要使用数组?如何定义数组? 如何引用数组元素? 2. 实验过程中遇到的问题及解决方法: 在最后一个试验中,在输出最大小标那里进行了仔细的思考,刚开始思考应 ...
- netstat 查看TCP状态值
一.TCP 状态值 netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}' CLOSED:无连接是活动的或正在进行LI ...
- HAL层Camera模块Dump图片--工作积累
Camera的raw data一般都是YUV420的格式,数据的特点是: YUV 4:2:0采样,每四个Y共用一组UV分量 YUV420格式: 先Y,后V,中间是U.其中的Y是w * h,U和V是w/ ...
- Mac终端命令行提示符格式更改方法
内容提要: 主要是通过~/.bash_profile文件更改环境变量PS1,修改命令行提示符的显示格式,并展示不同颜色. 本文介绍了默认设置的缺陷,以及需要用到的基础知识,最后介绍了更改命令行提示符格 ...
- python和pywin32实现窗口查找、遍历和点击
Pywin32是一个Python库,为python提供访问Windows API的扩展,提供了齐全的windows常量.接口.线程以及COM机制等等. 1.通过类名和标题查找窗口句柄,并获得窗口位置和 ...
- Javascript数据类型的一些注意点
1.字符串类型 substring()返回指定索引区间的子串: var s = 'hello, world' s.substring(0, 5); // 从索引0开始到5(不包括5),返回'hello ...
- 配置本地yum源
配置本地yum源 一. 远程挂载ISO文件 mount /dev/sr0 /mnt/cdrom (本地挂载mount -t iso9660 -o loop xx.iso /media) 二. 配置 ...
- 线上Linux服务器运维安全策略经验分享
线上Linux服务器运维安全策略经验分享 https://mp.weixin.qq.com/s?__biz=MjM5NTU2MTQwNA==&mid=402022683&idx=1&a ...
- Android 使用PullToRefresh实现下拉刷新和上拉加载(ExpandableListView)
PullToRefresh是一套实现非常好的下拉刷新库,它支持: 1.ListView 2.ExpandableListView 3.GridView 4.WebView 等多种常用的需要刷新的Vie ...