POJ 2255

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4
 5 struct node
 6 {
 7     char c;
 8     node * l;
 9     node * r;
10 };
11
12 node * make_tree(string pre,string ins);
13
14 void fun(node * root);
15
16 int main()
17 {
18     //freopen("acm.acm","r",stdin);
19     string pre;
20     string ins;
21     node * root;
22     while(cin>>pre>>ins)
23     {
24     //    cout<<pre<<" "<<ins<<endl;
25         root = new node;
26         root = make_tree(pre,ins);
27         fun(root);
28         cout<<endl;
29     }
30 }
31
32 node * make_tree(string pre,string ins)
33 {
34 //    cout<<" ------------ pre "<<pre<<"----------------- ins "<<ins<<endl;
35     node * root;
36     int index;
37     if(pre.length() > 0)
38     {
39         root = new node;
40         root->c = pre[0];
41         index = ins.find(pre[0]);
42         root->l = make_tree(pre.substr(1,index),ins.substr(0,index));
43         root->r = make_tree(pre.substr(index+1),ins.substr(index+1));
44     }
45     else
46         root = NULL;
47     return root;
48 }
49
50 void fun(node * root)
51 {
52     if(root->l)
53     {
54         fun(root->l);
55     }
56     if(root->r)
57     {
58         fun(root->r);
59     }
60     cout<<root->c;
61 }
时间: 2024-10-09 17:46:55

POJ 2255的相关文章

POJ 2255 Tree Recovery 二叉树恢复

一道和Leetcode的一道题目基本上一样的题目. 给出前序遍历和中序遍历序列,要求根据这些信息恢复一颗二叉树的原貌,然后按后序遍历序列输出. Leetcode上有给出后序和中序,恢复二叉树的. 不过其实算法都是一样的.仿佛又回到了做Leetcode题的那段岁月中了. 还有就是输入是我特别处理过的,就两个函数,大家会了的无视,不会的可以学习下. #include <stdio.h> #include <string> #include <algorithm> using

Tree Recovery POJ - 2255

Tree Recovery POJ - 2255 根据树的前序遍历和中序遍历还原后序遍历. (偷懒用了stl的find) 1 #include<iostream> 2 #include<string> 3 using namespace std; 4 string s1,s2; 5 int len; 6 void work(int l1,int r1,int l2,int r2) 7 { 8 if(l1==r1) 9 { 10 cout<<s1[l1]; 11 retu

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

链接:poj.org/problem?id=2255 题意: 分别给你一个二叉树的前序遍历序列和中序遍历序列,让你给出这个二叉树的后序遍历序列. 思路: 对于二叉树的三种遍历方式,都可以使用递归来实现,那么也一定可以使用递归来拆解,以达到从遍历序列确定二叉树具体结构的目的.对于前序遍历来说,第一个字母一定是根,并且在序列中根的左子树包含的点一定出现在根的右子树的前面.对于中序遍历序列来说,根前面出现的字母所代表的点一定出现在左子树中,根后面出现的字母所代表的点一定出现在右子树中.在根据前序与中序

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

http://poj.org/problem?id=2255 #include<cstdio> #include <cstring> using namespace std; const int maxn = 27; char pre[maxn],in[maxn]; char past[maxn]; void tre(int ps,int pe,int is,int ie,int& ind) { int lnum = strchr(in,pre[ps]) - in - is

POJ 2255 Tree Recoveryw(二叉树)

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

POJ - 2255 - Tree Recovery = 二叉树遍历

http://poj.org/problem?id=2255 题意:给定先序遍历和中序遍历,求后序遍历. 回忆以前上DataStructure课的时候貌似写过类似的. 先从先序入手,从左到右扫描,进入时节点时立刻入栈,离开节点时立刻出栈. 关键是怎么知道什么时候才是立刻节点了呢? 貌似只有n^2的做法,因为要从中序遍历序列中找根. 但其实假如预处理出中序遍历的序列中的字母每个指向的位置就不用这额外的时间花费n了. 也就是从先序遍历入手,进入节点时把根节点的中序遍历序列指针两侧递归下去. 所以构造

POJ 2255 Tree Recovery(树的遍历)

给定前序遍历和中序遍历,写出后序遍历. #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <assert.h> #include <algorithm> #define MAX 1234567890 #define MIN

poj 2255 Tree Recovery

Tree Recovery Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12413   Accepted: 7765 Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital le

POJ 2255 Tree Recovery 解题心得

原题: Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creations: D / / B E / \ / \ \ A C G / /

poj 2255 Tree Recovery 分治

Tree Recovery Description Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes. This is an example of one of her creations: D / / B E / \ / \ A