第七周 L2-006 樹的遍歷

題目:

給定一棵二叉樹的后序遍歷和中序遍歷,請(qǐng)你輸出其層序遍歷的序列。這里假設(shè)鍵值都是互不相等的正整數(shù)。

輸入格式:

輸入第一行給出一個(gè)正整數(shù)N(≤30),是二叉樹中結(jié)點(diǎn)的個(gè)數(shù)。第二行給出其后序遍歷序列。第三行給出其中序遍歷序列。數(shù)字間以空格分隔。

輸出格式:

在一行中輸出該樹的層序遍歷的序列。數(shù)字間以1個(gè)空格分隔,行首尾不得有多余空格。

輸入樣例:

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

輸出樣例:

4 1 6 3 5 7 2

ac代碼:

#include<iostream>
#include<queue>
#include<vector>
#define MAXSIZE 50
using namespace std;

int postorder[30], inorder[30], n;
vector<int> ans;
//bool visited[MAXSIZE] = { false };
typedef struct TNode* Node;
struct TNode {
    int num;
    Node left, right;
};
Node Build(int L1, int R1, int L2, int R2)
{
    if (L1 > R1)
        return NULL;
    Node root = new TNode;
    root->num = postorder[R2];//后序遍歷末位數(shù)
    int p = L1;
    while (inorder[p] != root->num)
        p++;
    int ln = p - L1;//左子樹節(jié)點(diǎn)數(shù)量
    root->left = Build(L1, p - 1, L2, L2 + ln - 1);     //L2,p-1);
    root->right = Build(p + 1, R1, L2 + ln, R2 - 1);    //p,R2-1);
    return root;
}

void BFS(Node root)
{
    queue<Node> Q;
    Q.push(root);
    while (!Q.empty())
    {
        Node u = Q.front();
        Q.pop();
        ans.push_back(u->num);
        if (u->left != NULL)
            Q.push(u->left);
        if (u->right != NULL)
            Q.push(u->right);
    }
}

int main()
{
    Node root = NULL;
    cin >> n;
    if (n == 0) {
        root = NULL;
        system("pause");
        return 0;
    }
    for (int i = 0; i < n; i++) {
        cin >> postorder[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> inorder[i];
    }
    root = Build(0, n - 1, 0, n - 1);
    BFS(root);
    for (int i = 0; i < ans.size(); i++)
    {
        if (i != 0)
            cout << " ";
        cout << ans[i];
    }
    cout << endl;
    system("pause");
    return 0;
}

在ac之前自己硬是把隊(duì)列的做法又寫了一遍

#include<iostream>
#define MAXSIZE 50
using namespace std;

int postorder[30], inorder[30], n;
typedef struct TNode* Node;
struct TNode {
    int num;
    Node left, right;
};
typedef struct queue {
    Node data[MAXSIZE];
    int front;
    int rear;
};

void CreateQueue(queue* Q) 
{
    Q->front = Q->rear = 0;
}

int EmptyQuene(queue* Q)
{
    if (Q->front == Q->rear)
        return 1;
    else return 0;
}

void EnQueue(queue *Q, Node n)
{
    if (Q->rear - Q->front == MAXSIZE - 1)
    {
        cout << "隊(duì)列已經(jīng)滿了" << endl;
        return;
    }
    else
    {
        Q->data[Q->rear] = n;
        Q->rear++;
    }
}

int DeQueue(queue* Q)
{
    if (EmptyQuene(Q) == 1)
        return -1;
    int n = Q->data[Q->front]->num;
    Q->front++;
    return n;
}

Node Front(queue *Q)
{
    return Q->data[Q->front];
}

Node Build(int L1, int R1, int L2, int R2)
{
    if (L1 > R1)
        return NULL;
    Node root = new TNode;
    root->num = postorder[R2];//后序遍歷末位數(shù)
    int p = L1;
    while (inorder[p] != root->num)
        p++;
    int ln = p - L1;//左子樹節(jié)點(diǎn)數(shù)量
    root->left = Build(L1, p - 1, L2, L2 + ln - 1);     //L2,p-1);
    root->right = Build(p + 1, R1, L2 + ln, R2 - 1);    //p,R2-1);
    return root;
}

void BFS(Node root)
{
    queue* Q = new queue;
    CreateQueue(Q);
    EnQueue(Q, root);
    while (!EmptyQuene(Q))
    {
        Node u = Front(Q);
        int k = DeQueue(Q);
        cout << k << " ";
        if (u->left != NULL)
            EnQueue(Q, u->left);
        if (u->right != NULL)
            EnQueue(Q, u->right); 
    }
}

int main()
{
    Node root = NULL;
    cin >> n;
    if (n == 0) {
        root = NULL;
        system("pause");
        return 0;
    }
    for (int i = 0; i < n; i++) {
        cin >> postorder[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> inorder[i];
    }
    root = Build(0, n - 1, 0, n - 1);
    BFS(root);
    cout << endl;
    system("pause");
    return 0;
}


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容