1. 程式人生 > 其它 >1020 Tree Traversals (25 分)(⼆叉樹的遍歷,後序中序轉層序)

1020 Tree Traversals (25 分)(⼆叉樹的遍歷,後序中序轉層序)

Description

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 (<=30), 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

生詞

英文 解釋
distinct 不同的
traversal 遍歷
corresponding 相應的

題目大意:

給定一棵二叉樹的後序遍歷和中序遍歷,請你輸出其層序遍歷的序列。這裡假設鍵值都是互不相等的正整數

分析:

與後序中序轉換為前序的程式碼相仿(無須構造二叉樹再進行廣度優先搜尋~),只不過加一個變數index,表示當前的根結點在二叉樹中所對應的下標(從0開始),所以進行一次輸出先序的遞迴過程中,就可以把根結點下標index及所對應的值儲存在map<int, int> level中,map是有序的會根據index從小到大自動排序,這樣遞迴完成後level中的值就是層序遍歷的順序~~

如果你不知道如何將後序和中序轉換為先序,請看-> https://www.liuchuo.net/archives/2090

原文連結:https://blog.csdn.net/liuchuo/article/details/52137796

題解

終於把這篇在11.04就編輯的博文抄完了。。。
學廢了之後發現也不是很難,可能忘太多了覺得太陌生了,有畏懼感嗚嗚
總而言之,還是要迎難而上!

#include <bits/stdc++.h>
const int maxn=50;
using namespace std;
struct node
{
    int data;
    node* lchild;
    node* rchild;
};
int pre[maxn],in[maxn],post[maxn];
int n;
node* create(int postL,int postR,int inL,int inR)
{
    if(postL>postR) return nullptr;
    node* root=new node;
    root->data=post[postR];
    int k;
    for(k=inL;k<=inR;k++){
        if(in[k]==post[postR]) break;
    }
    int numLeft=k-inL;
    root->lchild=create(postL,postL+numLeft-1,inL,k-1);
    root->rchild=create(postL+numLeft,postR-1,k+1,inR);
    return root;
}
int num=0;
void BFS(node* root){
    queue<node*> q;
    q.push(root);
    while(!q.empty()){
        node* now=q.front();
        q.pop();
        printf("%d",now->data);
        num++;
        if(num<n)printf(" ");
        if(now->lchild!=nullptr) q.push(now->lchild);
        if(now->rchild!=nullptr) q.push(now->rchild);
    }
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&post[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&in[i]);
    }
    node* root=create(0,n-1,0,n-1);
    BFS(root);
    return 0;
}

柳神程式碼(碼住,以後看!)

#include <cstdio>
#include <vector>
#include <map>
using namespace std;
vector<int> post, in;
map<int, int> level;
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++;
    level[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);
    auto it = level.begin();
    printf("%d", it->second);
    while(++it != level.end()) printf(" %d", it->second);
    return 0;
}

本文來自部落格園,作者:勇往直前的力量,轉載請註明原文連結:https://www.cnblogs.com/moonlight1999/p/15507553.html