跟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的更多相关文章

  1. Tree Recovery POJ - 2255

    Tree Recovery POJ - 2255 根据树的前序遍历和中序遍历还原后序遍历. (偷懒用了stl的find) #include<iostream> #include<st ...

  2. POJ 2255. Tree Recovery

    Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11939   Accepted: 7493 De ...

  3. POJ 2255 Tree Recovery 树的遍历,分治 难度:0

    http://poj.org/problem?id=2255 #include<cstdio> #include <cstring> using namespace std; ...

  4. Poj 2255 Tree Recovery(二叉搜索树)

    题目链接:http://poj.org/problem?id=2255 思路分析:根据先序遍历(如DBACEGF)可以找出根结点(D),其后为左右子树:根据中序遍历(如ABCDEFG),已知根结点(D ...

  5. poj 2255 Tree Recovery(求后序遍历,二叉树)

    版权声明:本文为博主原创文章,未经博主同意不得转载.vasttian https://blog.csdn.net/u012860063/article/details/37699219 转载请注明出处 ...

  6. POJ 2255 Tree Recovery && Ulm Local 1997 Tree Recovery (二叉树的前中后序遍历)

    链接:poj.org/problem?id=2255 本文链接:http://www.cnblogs.com/Ash-ly/p/5463375.html 题意: 分别给你一个二叉树的前序遍历序列和中序 ...

  7. POJ 2255 Tree Recoveryw(二叉树)

    题目原网址:http://poj.org/problem?id=2255 题目中文翻译: Description 小瓦伦丁非常喜欢玩二叉树. 她最喜欢的游戏是用大写字母构造的随机二叉树. 这是她的一个 ...

  8. POJ 2255 Tree Recovery(根据前序遍历和中序遍历,输出后序遍历)

    题意:给出一颗二叉树的前序遍历和中序遍历的序列,让你输出后序遍历的序列. 思路:见代码,采用递归. #include <iostream> #include <stdio.h> ...

  9. POJ 2255 Tree Recovery 二叉树的遍历

    前序和中序输入二叉树,后序输出二叉树:核心思想只有一个,前序的每个根都把中序分成了两部分,例如 DBACEGF ABCDEFG D把中序遍历的结果分成了ABC和EFG两部分,实际上,这就是D这个根的左 ...

随机推荐

  1. 【转】Xamarin.Android 入门之:Xamarin+vs2015 环境搭建

    Xamarin.Android 入门之:Xamarin+vs2015 环境搭建   一.前言 此篇博客主要写了如何使用搭建xamarin开发的环境,防止我自己万一哪天电脑重装系统了,可以直接看这篇博客 ...

  2. transform animation transition css3动画

    transform 定义   transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. 值 应用  如果transform与transition联合起 ...

  3. autowire异常的三个情况

    2010-3-11 16:06:00 net.sf.ehcache.config.ConfigurationFactory parseConfiguration 警告: No configuratio ...

  4. discuz函数解析--写日志

    public static function writelog($file, $log) { global $_G; $yearmonth = dgmdate(TIMESTAMP, 'Ym', $_G ...

  5. jquery rotate

    网上发现一个很有意思的jQuery旋转插件,支持Internet Explorer 6.0+ .Firefox 2.0 .Safari 3 .Opera 9 .Google Chrome,高级浏览器下 ...

  6. OpenSSL-for-IOS

    http://download.csdn.net/download/kokjuis/9723109

  7. 1069 Nim游戏

    1069 Nim游戏 基准时间限制:1 秒 空间限制:131072 KB 有N堆石子.A B两个人轮流拿,A先拿.每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜.假设A ...

  8. ArcGIS API for Silverlight 调用GP服务绘制等值面

    原文:ArcGIS API for Silverlight 调用GP服务绘制等值面 GP服务模型如下图: 示例效果图片如下:

  9. linux关机

    init 0; init是所有进程的祖先﹐它的进程号始终为1﹐所以发送TERM信号给init会终止所有的用户进程﹑守护进程等.shutdown 就是使用这种机制.init定义了8个运行级别(runle ...

  10. 实践JAVA wait(), notify(),sleep方法--一道多线程的面试题

    建立三个线程,A线程打印10次A,B线程打印10次B,C线程打印10次C,要求线程同时运行,交替打印10次ABC. 这个问题用Object的wait(),notify()就可以很方便的解决. publ ...