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这个根的左 ...
随机推荐
- Nginx return 关键字配置小技巧
Nginx的return关键字属于HttpRewriteModule模块: 语法:return http状态码 默认值:无 上下文:server,location,if 该指令将结束执行直接返回htt ...
- Bootstrap 模态框插件
一.基本使用 使用模态框的弹窗组件需要三层 div 容器元素,分别为 modal(模态声明层). dialog(窗口声明层).content(内容层).在内容层里面,还有三层,分别为 header(头 ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(11)-蓝牙串口代码分析
代码实例:点击打开链接 实现的功能是从uart口发送数据至另一个蓝牙串口,或是从蓝牙读取数据通过uart打印出数据. int main(void) { // Initialize leds_init( ...
- selenium webdriver
http://www.blogjava.net/qileilove/archive/2014/02/18/409975.html Selenium VS Webdriver Selen ...
- JavaScript判断文件的大小
function getFileSize(obj) {//obj 需要传入的参数为Input的对象 var objValue = obj.value; if (objValue == " ...
- Ubuntu-1404 GDB 调试C++报错
问题 Ubuntu1404下,当用GDB调试C++程序时,报错ImportError: No module named 'libstdcxx' 解决办法 vim ~/.gdbinit #~/.gdbi ...
- UITextView 监听 return key的改变
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSSt ...
- [转]AppCompat 22.1,Goole暴走,MD全面兼容低版本
AppCompat 22.1,Goole暴走,MD全面兼容低版本 分类: Android2015-04-24 09:48 1354人阅读 评论(0) 收藏 举报 android 目录(?)[+] ...
- stretchableImageWithLeftCapWidth 的使用方法
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight: (NSInteger)topCa ...
- linux web服务器静态资源的处理 unison+inotify双向同步
linux web服务器静态资源的处理 unison+inotify双向同步 http://monkeyzhu.blog.51cto.com/5764358/1324391 简介 unison可以使两 ...