1020 Tree Traversals (25 分)

1020 Tree Traversals (25 分)
 

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2


这道题我感觉像是在测你脑回路,挺有趣的,
这题我用bfs写的,先给你后序遍历(左右中)和中序遍历(左中右)。
也有看到一些别的题解,比如柳诺大神的题解,但是她的解法如果数据强点就过不去了。
所以后台数据确实比较水,但是用bfs的代码就不用担心数据量的问题了。
比如下面这组数据:
30
30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
这组数据她的代码绝对跑不了,因为2^30可是超了int,就算是vector也不可能开这么多,而且那样很浪费空间
用个bfs写出来就时间空间都解决了。

 1 #include <bits/stdc++.h>
 2 #define N 100
 3 using namespace std;
 4 int an[40], bn[40], n, cn[N],pos = 0;
 5 
 6 struct Node{
 7     int root, start, end;
 8 };
 9 queue<Node> q;
10 void dfs(int root, int start, int end){
11     Node node;
12     node.root = root, node.start = start,node.end = end;
13     q.push(node);
14     while(!q.empty()){
15         node = q.front();
16         q.pop();
17         if(node.start > node.end) 
18             continue;
19         cn[pos++] = an[node.root];
20         int i;
21         for(i = node.start; i < node.end; i++){
22             if(bn[i] == an[node.root])
23                 break;
24         }
25         q.push({node.root-(node.end-i)-1, node.start, i-1});
26         q.push({node.root-1, i+1, node.end}); 
27     }
28 }
29 
30 int main(){
31     cin >> n;
32     for(int i = 0; i < n; i++){
33         cin >> an[i];
34     }
35     for(int i = 0; i < n; i++){
36         cin >> bn[i];
37     }
38     memset(cn,-1,sizeof(cn));
39     dfs(n-1, 0, n-1);
40     for(int i = 0; i < pos; i++)
41         printf("%d%c",cn[i],i==pos-1?'\n':' ');
42     
43     return 0;
44 }

 

这里给了一下柳神的代码(虽然是错的,但是可以过题,想法还是挺好的):^v^(勿喷)

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct node {
    int index, value;
};
bool cmp(node a, node b) {
    return a.index < b.index;
}
vector<int> post, in;
vector<node> ans;
void pre(int root, int start, int end, int index) {
    if (start > end) return;
    int i = start;
    while (i < end && in[i] != post[root]) i++;
    ans.push_back({index, post[root]});
    pre(root - 1 - end + i, start, i - 1, 2 * index + 1);
    pre(root - 1, i + 1, end, 2 * index + 2);
}
int main() {
    int n;
    scanf("%d", &n);
    post.resize(n);
    in.resize(n);
    for (int i = 0; i < n; i++) scanf("%d", &post[i]);
    for (int i = 0; i < n; i++) scanf("%d", &in[i]);
    pre(n - 1, 0, n - 1, 0);
    sort(ans.begin(), ans.end(), cmp);
    for (int i = 0; i < ans.size(); i++) {
        if (i != 0) cout << " ";
        cout << ans[i].value;
    }
    return 0;
}
View Code

 

 
posted @ 2019-06-16 21:58  #忘乎所以#  阅读(340)  评论(0编辑  收藏  举报