PAT 1086 Tree Traversals Again

室友在做于是也做一发,跟已知两种遍历序列还原二叉树的思路类似,感觉PAT上的题目跟书本知识靠的近一些

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

void print_data(const vector<char> &as, const vector<int> &ns) {
    int len = ns.size();
    for (int i=0; i<len; i++) {
        if (as[i] == ‘i‘) {
            cout<<"push "<<ns[i]<<endl;
        } else {
            cout<<"pop"<<endl;
        }
    }
}

void build_postorder(vector<char> &actions, vector<int> &nums, int start, int end, vector<int> &postorder) {
    if (start >= end) return;
    if (actions[start] != ‘i‘) return;
    // root node
    postorder.push_back(nums[start]);

    // now try to find the seperator idx of
    // action(must be a pop pair wise with root node push) between two sub tree
    int pushs = 1;
    int idx = start + 1;
    while (pushs != 0 && idx < end) {
        if (actions[idx] == ‘i‘) {
            pushs++;
        } else {
            pushs--;
        }
        idx++;
    }

    // right sub tree
    build_postorder(actions, nums, idx, end, postorder);

    // left sub tree
    build_postorder(actions, nums, start + 1, idx, postorder);
}
int main() {

    int N, len;

    scanf("%d", &N);
    len = N * 2;

    char buf[10];
    int num;

    vector<char> actions(2 * N, ‘\0‘);
    vector<int> nums(2 * N, 0);

    // read in data
    for (int i=0; i<len; i++) {
        scanf("%s", buf);

        if (buf[3] == ‘h‘) {
            scanf("%d", &num);
            nums[i] = num;
            actions[i] = ‘i‘;    // push, input
        } else {
            actions[i] = ‘o‘;    // pop, output
        }
    }

    vector<int> postorder;

    build_postorder(actions, nums, 0, len, postorder);
    if (postorder.size() > 0){
        // print_data(actions, nums);
        for (int i=N - 1; i>=1; i--) {
            printf("%d ", postorder[i]);
        }
        printf("%d", postorder[0]);
    }

    return 0;
}
时间: 2024-10-11 13:42:36

PAT 1086 Tree Traversals Again的相关文章

PAT 1086 Tree Traversals Again (25)

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()

1086. Tree Traversals Again (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1086. Tree Traversals Again (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B 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

PAT 1020. Tree Traversals (25)

1020. Tree Traversals (25) 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.

PAT 1020 Tree Traversals

1020 Tree Traversals (25 分) 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

PAT 甲级 1086 Tree Traversals Again

https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 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

PAT Advanced 1086 Tree Traversals Again (25分)

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()

PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]

题目 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); po

PAT:1086. Tree Traversals Again (25) AC

#include<stdio.h> #include<string.h> #include<stack> using namespace std; const int MAX=50; int n,cnt=0; //n个节点,cnt在后序输出的时候控制空格数量用 int PRE[MAX],IN[MAX]; //先序,中序 int preI,inI; //先序,中序的下标 stack<int> S; //栈 struct node { int data; nod

PAT (Advanced Level) 1086. Tree Traversals Again (25)

入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<stack> using namespace std; const int maxn=1000+10; const int INF=0x7FFFFFFF; int n,tot; int Preorder[maxn],Inorder[maxn],Po