1020 Tree Traversals——PAT甲级真题

1020 Tree Traversals

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

题目大意:给你一颗二叉树的后序遍历序列和中序遍历序列。然后求出这颗二叉树的层次遍历序列。

大致思路:首先我们因该知道后序遍历序列的最后一个点为二叉树的根节点,在中序遍历序列中根节点左侧都为左子树,右侧都为右子树。所以我们要首先找到二叉树的根节点,然后在中序遍历中找到根节点所在的位置,然后对二叉树的左右子树递归的调用上述过程。

代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 35;
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
int postorder[N], inorder[N];  //后序遍历数组,中序遍历数组
int preorder[N];
int n;

//由后续和中序遍历创建二叉树
TreeNode* buildTree(int postL, int postR, int inL, int inR) {
    if (postL > postR)
        return NULL;  //设置递归返回条件,当postL ==
                      //postR时表明指向叶子节点,当postl > postR时,应当返回
    TreeNode* node = new TreeNode(postorder[postR]);
    //查找根节点在中序遍历中的位置
    int k;
    for (k = inL; k <= inR; k++) {
        if (inorder[k] == postorder[postR]) break;
    }
    int numL = k - inL;  //计算左子树结点的个数
    //中序遍历中根节点左边的是左子树,右边的是右子树递归建树
    node->left = buildTree(postL, postL + numL - 1, inL, k - 1);
    node->right = buildTree(postL + numL, postR - 1, k + 1, inR);
    return node;
}

//由前序和中序遍历创建二叉树
TreeNode* buildTree2(int preL, int preR, int inL, int inR) {
    if (preL > preR) return nullptr;
    TreeNode *node = new TreeNode(preorder[preL]);
    int k;
    for (k = inL; k <= inR; k++) {
        if (inorder[k] == preorder[preL]) break;
    }
    int numL = k - inL;
    node->left = buildTree2(preL + 1, preL + numL, inL, k - 1);
    node->right = buildTree2(preL + numL + 1, preR, k + 1, inR);
    return node;
}

//先序遍历
void inordervisit(TreeNode* root) {
    if (root == nullptr) return;
    cout << root->val << " ";
    inordervisit(root->left);
    inordervisit(root->right);
}

//层次遍历
void BFS(TreeNode* root) {
    queue<TreeNode*> q;
    q.push(root);
    int cnt++;
    while(!q.empty()) {
        auto node = q.front(); q.pop();
        cout << node->val;
        cnt++;
        if (cnt != n) cout << " ";
        else cout << endl; 
        if (node->left) q.push(node->left);
        if (node->right) q.push(node->right);
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &postorder[i]);
    for (int i = 1; i <= n; i++) scanf("%d", &inorder[i]);
    TreeNode* root = buildTree(1, n, 1, n);
    BFS(root);
    // inordervisit(root);
    return 0;
}

posted on 2021-02-08 21:23  翔鸽  阅读(53)  评论(0编辑  收藏  举报