【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)

题意:
输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列。

AAAAAccepted code:

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 stack<int>st;
 5 int a[37],b[37];
 6 int ans[37];
 7 void build(int n,int*a,int*b,int*ans){
 8     if(!n)
 9         return ;
10     int x=find(b,b+n,a[0])-b;
11     build(x,a+1,b,ans);
12     build(n-x-1,a+x+1,b+x+1,ans+x);
13     ans[n-1]=a[0];
14 }
15 int main(){
16     ios::sync_with_stdio(false);
17     cin.tie(NULL);
18     cout.tie(NULL);
19     int n;
20     cin>>n;
21     int cnt1=0,cnt2=0;
22     for(int i=1;i<=n+n;++i){
23         string s;
24         cin>>s;
25         int x;
26         if(s[1]=='u')
27             cin>>x;
28         if(s[1]=='u'){
29             a[cnt1++]=x;
30             st.push(x);
31         }
32         else{
33             b[cnt2++]=st.top();
34             st.pop();
35         }
36     }
37     build(n,a,b,ans);
38     for(int i=0;i<n;++i){
39         cout<<ans[i];
40         if(i<n-1)
41             cout<<" ";
42     }
43     return 0;
44 }
保持热爱 不懈努力 不试试看怎么知道会失败呢(划掉) 世上无难事 只要肯放弃(划掉)
原文地址:https://www.cnblogs.com/ldudxy/p/11879678.html