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(); 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
入栈push顺序为先序,出栈pop顺序为中序,根据先序中序序列还原二叉树,最终输出后序序列
#include<bits/stdc++.h>
using namespace std;
int n,x,in[40],pre[40],inI=0,preI=0;
stack<int> s;
struct node{
int data;
node* lchild;
node* rchild;
};
//申请内存 为所有字段赋初值
node* newNode(int v){
node* Node=new node;
Node->data=v;
Node->lchild=Node->rchild=NULL;
return Node;
}
node* create(int preL,int preR,int inL,int inR){
if(preL>preR) return NULL;
node* root=newNode(pre[preL]);
int k;//中序中根结点位置
for(k=inL;k<=inR;k++){
if(in[k]==pre[preL]) break;
}
int lnum=k-inL;//左子树长度
root->lchild=create(preL+1,preL+lnum,inL,k-1);
root->rchild=create(preL+lnum+1,preR,k+1,inR);
return root;
}
bool first=true;
void postOrder(node* root){
if(root==NULL) return;
postOrder(root->lchild);
postOrder(root->rchild);
if(!first) cout<<" ";
else first=false;
cout<<root->data;
}
int main(){
// freopen("in.txt","r",stdin);
string op;
cin>>n;
int T=2*n;
while(T--){
cin>>op;
if(op=="Push"){
cin>>x;
s.push(x);
pre[inI++]=x;
}else{
in[preI++]=s.top();
s.pop();
}
}
postOrder(create(0,n-1,0,n-1));
return 0;
}
注意,pat的输入可能不是很标准,根据‘ ’判断最后一组数据过不了。猜想也可能是最后一组数据最后没有换行,导致getline()读不进最后一个pop.总之,输入cin可以很好地过滤各种空格换行,不要投机取巧,标准输入输出求稳
bug代码
#include<bits/stdc++.h>
using namespace std;
int n,x,in[40],pre[40],inI=0,preI=0;
stack<int> s;
void print(int a[]){
for(int i=0;i<n;i++) cout<<a[i]<<" ";
cout<<endl;
}
struct node{
int data;
node* lchild;
node* rchild;
};
//申请内存 为所有字段赋初值
node* newNode(int v){
node* Node=new node;
Node->data=v;
Node->lchild=Node->rchild=NULL;
return Node;
}
node* create(int preL,int preR,int inL,int inR){
if(preL>preR) return NULL;
node* root=newNode(pre[preL]);
int k;//中序中根结点位置
for(k=inL;k<=inR;k++){
if(in[k]==pre[preL]) break;
}
int lnum=k-inL;//左子树长度
root->lchild=create(preL+1,preL+lnum,inL,k-1);
root->rchild=create(preL+lnum+1,preR,k+1,inR);
return root;
}
bool first=true;
void postOrder(node* root){
if(root==NULL) return;
postOrder(root->lchild);
postOrder(root->rchild);
if(!first) cout<<" ";
else first=false;
cout<<root->data;
}
int main(){
// freopen("in.txt","r",stdin);
string op;
cin>>n;
cin.get();
int T=2*n;
while(T--){
getline(cin,op);
if(op.find(' ')!=string::npos){
x=op[op.length()-1]-'0';
s.push(x);
pre[inI++]=x;
}else{
in[preI++]=s.top();
s.pop();
}
}
node* root=create(0,n-1,0,n-1);
postOrder(root);
return 0;
}
来源:CSDN
作者:树叶子_
链接:https://blog.csdn.net/hza419763578/article/details/100527729