uva 536

给出二叉树先序和中序遍历打印出后序遍历结果。

这一题跟之前的 uva 548有一些类似,可以对照着学习

代码如下,特备注意dfs的时候递归条件心里要清楚,否则出错很麻烦:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <set>
using namespace std;

const int maxn = 1000 + 10;

string pre_order,in_order;
int lch[maxn],rch[maxn];

int build(int l1,int r1,int l2,int r2){
    if(l1>r1) return 0;
    int root = pre_order[l1]-‘A‘+1;
    int p = l2;
    while(in_order[p]-‘A‘+1 != root) p++;
    int cnt = p-l2;
    lch[root] = build(l1+1,l1+cnt,l2,p-1);
    rch[root] = build(l1+cnt+1,r1,p+1,r2);
    return root;
}
void solve(int root){
    if(lch[root]) solve(lch[root]);
    if(rch[root]) solve(rch[root]);
    char ch = ‘A‘+root-1;
    cout<<ch;
}

int  main(){
    while(cin >> pre_order ){
        memset(lch,0,sizeof(lch));
        memset(rch,0,sizeof(rch));
        cin >> in_order;
        int len = pre_order.length();
        build(0,len-1,0,len-1);
        solve(pre_order[0]-‘A‘+1);
        cout<<endl;
        /*for(int i = 0;i<10;i++){
            cout<<i<<":"<<lch[i]<<","<<rch[i]<<endl;
        }*/

    }
    return 0;
} 
时间: 2024-10-09 11:32:12

uva 536的相关文章

UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)

传送门1: https://uva.onlinejudge.org/external/5/536.pdf 传送门2: http://acm.gdufe.edu.cn/Problem/read/id/1077 题意一样   输入不一样 HINT: 1. Preorder : (root, left subtree, right subtree); Inorder : (left subtree, root, right subtree); Postorder: (left subtree, rig

UVa 536 Tree Recovery(先序,中序求后序)

题意  给你二叉树的先序序列和中序序列  求它的后序序列 先序序列的第一个一定是根  中序序列根左边的都属于根的左子树  右边的都属于右子树  递归建树就行了 #include <bits/stdc++.h> using namespace std; typedef struct TNode { char data; TNode *lc, *rc; } node, *BTree; void build(BTree &t, string pre, string in) { t = new

UVA 536 TreeRocvery

根据先序历遍和中序历遍输出后序历遍,并不需要真的建树,直接递归解决 #include<cstdio> #include<cstring> const int N = 30; char preOrder[N]; char midOrder[N]; char S[N]; int top; void solve(char *pre,char *mid,int len)//len>=1 { S[++top] = *pre; char *p = mid; while(*p != *pr

Uva 536 Tree Recovery

1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 string pr,in; 6 string postorder; 7 void process(string preord,string inord); 8 int main() 9 { 10 //freopen("D:\\acm.txt","r",stdin); 11 while(cin>>

UVA 536 Tree Recovery(由前序中序求后序,经典)

Tree Recovery Time Limit: 3000 MS 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 /

二叉树基础练习

前序遍历(先根遍历):根,左子树,右子树 中序遍历:左子树,根,右子树后序遍历:左子树,右子树,根 先序遍历:ABDECF 中序遍历:DBEAFC 后序遍历:DEBFCA 层次遍历:ABCDEF UVA 112  Tree Summing 题目:给你一个数和一棵树,问是否存在根到叶子的路径使得路径上的数字和与已知数相等. 注意数据中可能有负数 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #i

【UVA】536 Tree Recovery(树型结构基础)

题目 题目 ? ? 分析 莫名A了 ? ? 代码 #include <bits/stdc++.h> using namespace std; string s1,s2; void build(int l1,int r1,int l2,int r2) { int root=l1,p=l2; if(l1>r1) return; while(s2[p]!=s1[root] && p<=r2) p++; int cnt=p-l2; build(l1+1,l1+cnt,l2,

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te