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


Figure 1

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
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <string>
 8 #include <stack>
 9 #include <queue>
10 using namespace std;
11 const int maxn=33;
12  int n;
13 int pre[maxn],in[maxn];
14 struct node{
15     int data;
16     node *left;
17     node *right;
18 };
19 //给一个先序和中序,求层次遍历
20 node *create(int a,int b,int c,int d)
21 {
22       if(a>b)return NULL;
23       node *temp=new node;
24       temp->data=pre[a];
25       int k=0;
26       for(;k+c<=d;k++)
27       {
28         if(in[c+k]==pre[a])break;
29     }
30     temp->left=create(a+1,a+k,c,c+k-1);
31     temp->right=create(a+k+1,b,c+k+1,d);
32     return temp;
33 }
34 //后序
35 int printcount=0;
36 void postorder(node * root)
37 {
38     if(root==NULL)return;
39     postorder(root->left);
40     postorder(root->right);
41     printf("%d",root->data);
42     printcount++;
43     if(printcount<n)printf(" ");
44  }
45 void BFS(node * root)
46 {
47     int count=0;
48   queue<node*> q;
49   q.push(root);
50   while(!q.empty())
51   {
52    node* now=q.front();
53    q.pop();
54    printf("%d",now->data);
55    count++;
56    if(count<n)printf(" ");
57    if(now->left!=NULL)q.push(now->left);
58    if(now->right!=NULL)q.push(now->right);
59   }
60 }
61 int main(){
62     int temp,num=0,innum=0;
63     stack<int> st;
64     scanf("%d",&n);
65     for(int i=0;i<n*2;i++)
66     {
67      char a[5];
68      scanf("%s",a);
69      if(strcmp(a,"Push")==0)
70      {
71          scanf("%d",&temp);
72          pre[num++]=temp;
73          st.push(temp);
74      }//pop
75      else
76      {
77          in[innum++]=st.top();
78          st.pop();
79      }
80     }
81     //根据前序中序 建树
82
83     node* root=create(0,n-1,0,n-1);
84     postorder(root);
85     return 0;
86 }
时间: 2024-08-16 15:37:22

A1086. Tree Traversals Again (25)的相关文章

二叉树遍历 A1086.Tree Traversals Again(25) (转化为给定前序和中序列,求后序)

#include <bits/stdc++.h> #include <stdio.h> #include <stdlib.h> #include <queue> using namespace std; const int maxn = 50; struct node{ int data; node* lchild; node* rchild; }; int pre[maxn],in[maxn],post[maxn]; int n; node* create

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