03-树2. Tree Traversals Again (25)

根据姥姥提示,入栈顺序就是先序遍历,出栈顺序就是中序遍历。然后在用solve函数进行递归分治来求出后序遍历的结果。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include<stack>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int pre[50],in[50],post[50];

void solve(int prel,int inl,int postl,int m)
{
    int i;
    if(m==0) return;
    else if(m==1) {post[postl]=pre[prel];return;}
    else post[postl+m-1]=pre[prel];
    for(i=0;i<m;i++)
    {
        if(in[i+inl]==pre[prel]) break;
    }
    solve(prel+1,inl,postl,i);
    solve(prel+i+1,inl+i+1,postl+i,m-i-1);
}

int main()
{
    int i,t,j,k,n;
    char c[10];
    stack<int>q;
    while(~scanf("%d",&n))
    {
        while(!q.empty()) q.pop();
        j=k=0;
        for(i=1; i<=2*n; i++)
        {
            scanf("%s",c);
            if(strcmp(c,"Push")==0)
            {
                scanf("%d",&t);
                q.push(t);
                pre[j++]=t;
            }
            else
            {
                t=q.top();
                in[k++]=t;
                q.pop();
            }
        }
        solve(0,0,0,n);
        for(i=0; i<n; i++)
        {
            if(!i)printf("%d",post[i]);
            else printf(" %d",post[i]);
        }

        printf("\n");
    }
    return 0;
}
时间: 2024-09-28 03:03:28

03-树2. 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

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

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

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

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