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.
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
解题思路:入栈的序列为先序遍历顺序,出队的顺序为中序遍历顺序,要求输出后序遍历顺序。先对输入的字符串进行处理,将先序,中序序列存在数组中再进行操作。
代码
#include<cstdio>
#include<string>
#include<stack>
#include<iostream>
using namespace std;
stack<int> st;
int in[50],pre[50];
int flag = 0;
struct node{
int data;
node* lchild;
node* rchild;
};
node* create(int preL,int preR,int inL,int inR){
if(preL > preR){
return NULL;
}
node* root = new node;
root->data = pre[preL];
int k;
for(k = inL;k < inR;k++){
if(pre[preL] == in[k]){
break;
}
}
int leftnum = k - inL;
root->lchild = create(preL+1,preL+leftnum,inL,inL+leftnum - 1);
root->rchild = create(preL+leftnum+1,preR,inL+leftnum+1,inR);
return root;
}
void postorder(node* root){
if(root == NULL)
return;
postorder(root->lchild);
postorder(root->rchild);
if(flag)
printf(" ");
flag = 1;
printf("%d",root->data);
}
int str_to_num(string s)
{
int result = 0;
int a = 1;
for(int i = s.length() - 1;i>=0;i--){
result += (s[i] - '0')*a;
a*=10;
}
return result;
}
int main(void)
{
int N,num,inpos = 0,prepos = 0;
scanf("%d",&N);
getchar();
for(int i = 0;i < 2*N;i++){
string temp,strnum;
getline(cin,temp);
if(temp.length()>3){
strnum = temp.substr(5);
num = str_to_num(strnum);
st.push(num);
pre[prepos++] = num;
}
else{
num = st.top();
in[inpos++] = num;
st.pop();
}
}
node* root = create(0,N - 1,0,N - 1);
postorder(root);
return 0;
}
下面用静态链表实现,用结构体数组存储地址,要注意用temp存储index否则index会在递归的过程中改变,导致错误。
#include<cstdio>
#include<string>
#include<stack>
#include<iostream>
using namespace std;
stack<int> st;
int in[50],pre[50];
int flag = 0;
struct node{
int data;
int lchild;
int rchild;
}Node[100];
int index = 0;
int create(int preL,int preR,int inL,int inR){
if(preL > preR){
return -1;
}
int temp = index;
index++;
Node[temp].data = pre[preL];
int k;
for(k = inL;k < inR;k++){
if(pre[preL] == in[k]){
break;
}
}
int leftnum = k - inL;
Node[temp].lchild = create(preL+1,preL+leftnum,inL,inL+leftnum - 1);
Node[temp].rchild = create(preL+leftnum+1,preR,inL+leftnum+1,inR);
return temp;
}
void postorder(int root){
if(root == -1)
return;
postorder(Node[root].lchild);
postorder(Node[root].rchild);
if(flag)
printf(" ");
flag = 1;
printf("%d",Node[root].data);
}
int str_to_num(string s)
{
int result = 0;
int a = 1;
for(int i = s.length() - 1;i>=0;i--){
result += (s[i] - '0')*a;
a*=10;
}
return result;
}
int main(void)
{
int N,num,inpos = 0,prepos = 0;
scanf("%d",&N);
getchar();
for(int i = 0;i < 2*N;i++){
string temp,strnum;
getline(cin,temp);
if(temp.length()>3){
strnum = temp.substr(5);
num = str_to_num(strnum);
st.push(num);
pre[prepos++] = num;
}
else{
num = st.top();
in[inpos++] = num;
st.pop();
}
}
int root = create(0,N - 1,0,N - 1);
postorder(root);
return 0;
}