二叉樹

根據後續遍歷和中序遍歷建樹:

樹的遍歷

時間限制
400 ms
內存限制
65536 kB
代碼長度限制
8000 B
判題程序
Standard
作者
陳越

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

輸入格式:

輸入第一行給出一個正整數N(<=30),是二叉樹中結點的個數。第二行給出其後序遍歷序列。第三行給出其中序遍歷序列。數字間以空格分隔。

輸出格式:

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

輸入樣例:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
輸出樣例:
4 1 6 3 5 7 2
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
int hou[35], zhong[35];
int num;
struct node {
    int n;
    struct node *left;
    struct node *right;
};
struct node *creatNode(int n) {
    struct node *tmp = (struct node *) malloc(sizeof(struct node));
    tmp->n = n;
    tmp->left = tmp->right = NULL;
    return tmp;
};
struct node *creatTree(int l, int r, int ll, int rr) { //ll和rr表示中根遍歷的下標,l和r表示後續遍歷的下標
    struct node *root = NULL;
    int pos;
    if(ll <= rr) {
        for(int i = ll; i <= rr; i++) {
            if(zhong[i] == hou[r]) {
                pos = i;
                break;
            }
        }
        root = creatNode(hou[r]);
    }
    if(root) {
        root->left = creatTree(l, l+pos-ll-1, ll, pos-1);
        root->right = creatTree(l+pos-ll, r-1, pos+1, rr);
    }
    return root;
};
void leavel(struct node *root) {
    if(!root) return ;
    printf("%d", root->n);
    queue<struct node *> q;
    q.push(root);
    while(!q.empty()) {
        struct node *t = q.front();
        q.pop();
        if(t) {
            if(t->left) {
                q.push(t->left);
                printf(" %d", t->left->n);
            }
            if(t->right) {
                q.push(t->right);
                printf(" %d", t->right->n);
            }
        }
    }
}
int main() {
    scanf("%d", &num);
    for(int i = 0; i < num; i++) {
        scanf("%d", &hou[i]);
    }
    for(int i = 0; i < num; i++) {
        scanf("%d", &zhong[i]);
    }
    struct node * root = creatTree(0, num-1, 0, num-1);
    leavel(root);
    return 0;
}

根據前序遍歷序列和中序遍歷序列建樹:

 玩轉二叉樹

時間限制
400 ms
內存限制
65536 kB
代碼長度限制
8000 B
判題程序
Standard
作者
陳越

給定一棵二叉樹的中序遍歷和前序遍歷,請你先將樹做個鏡面反轉,再輸出反轉後的層序遍歷的序列。所謂鏡面反轉,是指將所有非葉結點的左右孩子對換。這裏假設鍵值都是互不相等的正整數。

輸入格式:

輸入第一行給出一個正整數N(<=30),是二叉樹中結點的個數。第二行給出其中序遍歷序列。第三行給出其前序遍歷序列。數字間以空格分隔。

輸出格式:

在一行中輸出該樹反轉後的層序遍歷的序列。數字間以1個空格分隔,行首尾不得有多餘空格。

輸入樣例:
7
1 2 3 4 5 6 7
4 1 3 2 6 5 7
輸出樣例:
4 6 1 7 5 3 2
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
int qian[35], zhong[35];
int num;
struct node {
    int n;
    struct node *left;
    struct node *right;
};
struct node *creatNode(int n) {
    struct node *tmp = (struct node *) malloc(sizeof(struct node));
    tmp->n = n;
    tmp->left = tmp->right = NULL;
    return tmp;
};
struct node *creatTree(int l, int r, int ll, int rr) {//ll和rr代表後序遍歷的序號,l和r代表前序遍歷的序號
    struct node *root = NULL;
    int pos;
    if(ll <= rr) {
        for(int i = ll; i <= rr; i++) {
            if(zhong[i] == qian[l]) {
                pos = i;
                break;
            }
        }
        root = creatNode(qian[l]);
    }
    if(root) {
        root->left = creatTree(l+1, l+pos-ll, ll, pos-1);
        root->right = creatTree(l+pos-ll+1, r, pos+1, rr);
    }
    return root;
};
void leavel(struct node *root) {
    if(!root) return ;
    printf("%d", root->n);
    queue<struct node *> q;
    q.push(root);
    while(!q.empty()) {
        struct node *t = q.front();
        q.pop();
        if(t) {
            if(t->right) {
                q.push(t->right);
                printf(" %d", t->right->n);
            }
            if(t->left) {
                q.push(t->left);
                printf(" %d", t->left->n);
            }
        }
    }
}
int main() {
    scanf("%d", &num);
    for(int i = 0; i < num; i++) {
        scanf("%d", &zhong[i]);
    }
    for(int i = 0; i < num; i++) {
        scanf("%d", &qian[i]);
    }
    struct node * root = creatTree(0, num-1, 0, num-1);
    leavel(root);
    return 0;
}






發佈了40 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章