Tree Traversals Again (25)

这题在于分治法的利用

#include <iostream>
#include <string>
#include <stack>
using namespace std;

void fun(int *pre, int *mid, int len, int * result);
int main()
{
    int n;
    int *a, *b;
    int x = 0, top = 0, y = 0;
    stack<int> S;

    cin >> n;
    a = (int*)malloc(n*sizeof(int));
    b = (int*)malloc(n*sizeof(int));
    for (int i = 0; i < 2 * n; i++){
        string str;
        cin >> str;
        if (str == "Push"){
            cin >> a[x];
            S.push(a[x]);
            x++;
        }
        else{
            b[y] = S.top();
            S.pop();
            y++;
        }
    }

    int *result = (int *)malloc(n*sizeof(int));
    fun(a, b, n,result);

    cout << result[0];
    for (int i = 1; i < n; i++){
        cout << " " << result[i];
    }

}
void fun(int *pre, int *mid, int len,int * result)
{
    if (len == 0){
        return;
    }
    if (len == 1){
        result[0] = pre[0];
        return;
    }
    int p = pre[0];
    result[len - 1] = p;

    int i;
    for (i = len - 1; i >= 1; i--){
        if (mid[i] == p){
            break;
        }
    }
    fun(pre + 1, mid, i, result);
    fun(pre + 1 + i, mid + 1 + i, len - 1 - i, result + i);
}
时间: 2024-11-03 21:02:48

Tree Traversals Again (25)的相关文章

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

pat1086. Tree Traversals Again (25)

1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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 th

03-树3. Tree Traversals Again (25)

03-树3. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 th

03-3. Tree Traversals Again (25)

03-3. 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:

pat03-树3. Tree Traversals Again (25)

03-树3. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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 th

A1086. 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 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)

时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue push 的顺序就是二叉树的前序 pop的顺序就是二叉树的中序遍历 本质上还是考根据这两个顺序建立二叉树,并且进行后序遍历 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when

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. Input Specification: Each

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