1086 Tree Traversals Again——PAT甲级真题

1086 Tree Traversals Again

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.Snip20160811_80

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: “Push X” where X is the index of the node being pushed onto the stack; or “Pop” meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
Sample Output:
3 4 2 6 5 1

题目大意:用栈来模拟以可二叉树的中序遍历,让你求出这棵二叉树的后序遍历。

大致思路:由题意可知二叉树的输入顺序为二叉树的前序遍历,然后我们借助一个栈来得出二叉树的后续遍历。进行一次Push操作我们就像栈中存一个元素,Pop操作我们就返回栈顶元素并将元素存入数组中。最后得到二叉树的中序遍历序列。

#include <bits/stdc++.h>

using namespace std;

const int N = 65;
struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};
int n;
TreeNode root[N];
vector<int> preorder, inorder, postorder;  //先序序列,中序序列

void newNode(int x) {
    root[x].val = x;
    root[x].left = root[x].right = -1;
}

//给出了中序和先序构建二叉树
TreeNode* create(int preL, int preR, int inL, int inR) {
    if (preL > preR) return -1;
    TreeNode* root = new TreeNode(preorder[preL]);
    int k;
    for (k = inL; k <= inR; k++) {
        if (inorder[k] == preorder[preL]) break;
    }
    int numL = k - inL;
    root->left = create(preL + 1, preL + numL, inL, k - 1);
    root->right = create(preL + numL + 1, preR, k + 1, inR);
    return root;

}

void postvisit(TreeNode* root) {
    if (node == -1) return;
    postvisit(root->left);
    postvisit(root->right);
    postorder.push_back(root->val);
}

int main() {
    scanf("%d", &n);
    //先将每个节点初始化
    stack<int> tmp;
    string op;
    int id;
    while(n--) {
        cin >> op;
        if (op == "Push") {
            cin >> id;
            tmp.push(id);
            preorder.push_back(id);
        }
        else {
            inorder.push_back(tmp.top());
            tmp.pop();
        }
    }
    create(0, n - 1, 0, n - 1);
    TreeNode* root = create(0, n - 1, 0 , n - 1);
    postvisit(root);
    for (int i = 0; i < postorder.size(); i++) {
        cout << postorder[i];
        if (i != postorder.size() - 1) cout << " ";
        else cout << endl;
    }
    return 0;
}

posted on 2021-02-09 17:00  翔鸽  阅读(53)  评论(0编辑  收藏  举报