PAT甲级1127 ZigZagging on a Tree先后序建立树

1127

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences. And it is a simple standard routine to print the numbers in level-order. However, if you think the problem is too simple, then you are too naive. This time you are supposed to print the numbers in "zigzagging order" -- that is, starting from the root, print the numbers level-by-level, alternating between left to right and right to left. For example, for the following tree you must output: 1 11 5 8 17 12 20 15.

zigzag.jpg

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 inorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:
For each test case, print the zigzagging sequence of the tree in a line. 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:
8
12 11 20 17 1 15 8 5
12 20 17 11 15 8 5 1
结尾无空行
Sample Output:
1 11 5 8 17 12 20 15
结尾无空行

题目:

对一棵树进行之字形遍历

输入

节点个数
中序序列
后序序列

计算节点所在层次的方法是直接设立一个全局变量level,初始化为0,在递归过程中,开头自增,结尾自减,相当于每次深度遍历(每次递归、每到下一层的子节点就+1,回到上一层就-1),因此这个节点所在的层就等于level。递归结束后就自动计算好每个节点的层次了

zigzag比较麻烦,需要在层次遍历上记录每一层的节点,最后再level % 2 == 0来交替输出的顺序

因为给出的值并不是连续的,也不是和索引有关的,因此用指针的形式进行建立树
(如果给出的是连续的,也是和索引有关的,那么就可以
struct node{
int data;
int lchild,rchild;
};
vector tree;

步骤

1、根据中序和后序建立树,并对每个节点记录层次(build的递归每次递归会遍历左右孩子节点,也就是遍历下一层的节点,因此每次递归都是在某一个层,进入递归时增加全局变量层数,把该次递归的层数记录在节点中,跳出递归后减少层数)
2、对树进行层次遍历,记录每层的输出节点(从左往右)
3、根据2的结果对level % 2 == 0来交替输出的顺序

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
struct node {
    int data, depth;
    node* lchild;
    node* rchild;
}tree[100];
int n;
int loc = 0;
vector<int> post;
vector<int> in;
node* creat() {
    tree[loc].lchild = tree[loc].rchild = nullptr;
    return &tree[loc++];
}
int flag = 0;
node* build(int s1, int e1, int s2, int e2) {
    flag++;
    node* root = creat();
    root->data = post[e2];
    root->depth = flag;
    int rootidx;
    for (int i = s1; i <= e1; ++i) {
        if (in[i] == post[e2]) {
            rootidx = i;
            break;
        }
    }
    //exist left
    if (rootidx != s1) {
        root->lchild = build(s1, rootidx - 1, s2, s2 + rootidx - s1 - 1);
    }
    //exist right
    if (rootidx != e1) {
        root->rchild = build(rootidx + 1, e1, s2 + rootidx - s1, e2 - 1);
    }
    flag--;
    return root;
}
vector<int> res[35];
int max_floor = 0;
void zigzag(node* root) {
    queue<node*> q;
    q.push(root);
    while (!q.empty()) {
        node* temp = q.front();
        q.pop();
        res[temp->depth].push_back(temp->data);
        if (max_floor < temp->depth) {
            max_floor = temp->depth;
        }
        if (temp->lchild != nullptr) {
            q.push(temp->lchild);
        }
        if (temp->rchild != nullptr) {
            q.push(temp->rchild);
        }
    }
}
void preorder(node* root) {
    cout << root->data << ' ' << root->depth << endl;
    if (root->lchild != nullptr) {
        preorder(root->lchild);
    }
    
    if (root->rchild != nullptr) {
        preorder(root->rchild);
    }
}
int main() {
    cin >> n;
    post.resize(n);
    in.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> in[i];
    }
    for (int i = 0; i < n; ++i) {
        cin >> post[i];
    }
    node* root = build(0, n - 1, 0, n - 1);
    zigzag(root);
    cout << res[1][0];
    for (int i = 2; i <= max_floor; ++i) {
        if (i % 2 == 0) {
            for (int j = 0; j < res[i].size(); ++j) {
                cout << ' ' << res[i][j];
            }
        }
        else {
            for (int j = res[i].size() - 1; j >= 0; --j) {
                cout << ' ' << res[i][j];
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值