PAT1020 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 (≤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

分析題目

日常查單詞環節:postorder and inorder traversal sequences 後序和中序遍歷序列
level order traversal sequence 層序遍歷序列
根據題意,這是一個已知後序和先序遍歷序列求層序遍歷序列的題目

先去學習了一下柳神的關於已知後序與中序輸出前序

後序中序輸出前序

代碼

首先說明幾個變量
root 根結點的位置(後序) i 根節點的位置(中序)
b 樹開始遍歷的位置(中序) e 樹結束遍歷的位置(中序)

後序爲左右根,中序爲左根右
後序序列中最後一個肯定是樹的根結點,從最後一個開始,將中序分成兩撥。
左子樹遍歷的起始點爲b, 結束點爲i-1,根節點爲root-1-(e-i)
右子樹遍歷的起始點爲i+1,結束點爲e,根節點爲root-1

#include <iostream>
using namespace std;
int post[] = {3, 4, 2, 6, 5, 1};//後序序列
int in[] = {3, 2, 4, 1, 6, 5};//中序序列

void pre(int b, int e, int root)
{
    if(b > e)
        return;
    int i = b;
    while(i< e && post[root] != in[i]) i++;
    cout << post[root] << " " << i << " " << e << endl;
    pre(b, i-1, root-1-(e-i));
    cout << "i: " << i << " b: " << b << " e: " << e << " root:" << root << endl;
    pre(i+1, e, root-1);
}

int main()
{
    pre(0, 5, 5);
    return 0;
}

好了學習完成,我們來看這道題,這題是一個已知後序和中序遍歷序列求層序遍歷序列的題目,思路是一樣的。

後序爲左右根,中序爲左根右
後序序列中最後一個肯定是樹的根結點,從最後一個開始,將中序分成兩撥。
左子樹遍歷的起始點爲b, 結束點爲i-1,根節點爲root-1-(e-i)
右子樹遍歷的起始點爲i+1,結束點爲e,根節點爲root-1

爲了輸出層序,則另設一個變量index跟蹤其層數

代碼

#include <iostream>
#include <vector>
using namespace std;
vector<int> post, in, level(100000, -1);
void pre(int root, int b, int e, int index)
{
    int i = b;
    if(b > e)
        return;
    while(i < e && post[root]!= in[i]) i++;
    level[index] = post[root];
    pre(root-1-(e-i), b, i-1, index*2+1);
    pre(root-1, i+1, e, index*2+2);
}


int main()
{
    int N;
    int cnt = 0;
    cin >> N;
    post.resize(N);
    in.resize(N);
    for(int i = 0; i<N; i++) cin >> post[i];
    for(int i = 0; i<N; i++) cin >> in[i];
    pre(N-1, 0, N-1, 0);
    for(int i = 0; i<level.size(); i++)
    {
        if(level[i] != -1)
        {
            if(cnt!= 0)
                cout << " ";
            cout << level[i];
            cnt++;
        }
        if(cnt == N)
            break;
    }
    return 0;
}

總結:

柳神太強了,但有個點沒弄懂,就是如果用index去記錄結點在樹中的位置,題目給的邊界條件爲N<=30,按理說,最大應是2^30,這個數遠大於代碼裏的100000,但還是能夠AC了,這個地方沒太懂。
這題典型的已知樹的其中兩個序列,求第三個序列
通過柳神代碼裏還學會了vector中resize的用法,和初始化一個固定長並填入初始值的方法,這樣使得代碼看起來更加簡潔高效。
每日吹柳神

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章