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, right subtree, root);

2. 对于preorder,第一个元素即为整棵树的根
  且在preorder中,待求子树结点靠前为根结点

3. 以找到的点为界,将inorder划分为两棵子树

#include <bits/stdc++.h>
using namespace std;

string pre, in;
int length;
char ans[30];

void process(int l, int r){
    if(l > r) return;
    bool flag = false;
    char ch;
    int mid;
    for(int i = 0; i < pre.length(); ++i){
        for(int j = l; j <= r; ++j){
            if(pre[i] == in[j]){
                flag = true;
                ch = pre[i];
                mid = j;
                break;
            }
        }
        if(flag) break;
    }
    ans[--length] = ch;

  //注意先右后左(后序遍历从左到右再到根)
    process(mid + 1, r);
    process(l, mid - 1);
}

int main(){
    while(cin >> pre >> in){
        length = pre.length();
        process(0, length - 1);
        for(int i = 0; i < pre.length(); ++i) cout << ans[i];
        cout << endl;
    }
    return 0;
}

另一种做法是建树模拟,贴上小光师兄博客里这道题的链接: http://blog.csdn.net/u012469987/article/details/41294313

时间: 2024-10-11 22:46:23

UVa 536 Tree Recovery | GOJ 1077 Post-order (习题 6-3)的相关文章

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 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 /

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(树型结构基础)

题目 题目 ? ? 分析 莫名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,

[2016-02-08][UVA][548][Tree]

UVA - 548 Tree Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root

UVA - 112 - Tree Summing (数的求和!栈的应用!)

UVA - 112 Tree Summing Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Tree Summing  Background LISP was one of the earliest high-level programming languages and, with FORTRAN, is one of the oldest

POJ 2255 Tree Recovery 二叉树恢复

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

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

poj2255 Tree Recovery

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