description
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<iostream>
#include<string>
#include<stack>
using namespace std;
struct TreeNode
{
int data,left,right; //结点
}tn[36];
int BuildTree() //建树
{
int n;
cin>>n;
stack<int>s;
string str;
int root=-1,fa,d,left=1; //root表示根初始化为-1 left用于判断此时是左结点还是右子结点 因为中序遍历,先是左结点,初始化为1
for(int i=1;i<=2*n;i++)
{
cin>>str;
if(str[1]=='u') //判断第二位就行
{
cin>>d;
if(root==-1) //第一个输入的是根结点
root=d;
else
{ //每次push的都是上一fa的子节点
if(left) //left等于1表示左子结点 0表示右
tn[fa].left=d;
else
tn[fa].right=d;
}
fa=d; //fa更新为这一轮的子节点
left=1; //left设为1,如果下一轮要push肯定是左面
s.push(d); //加入队列中 之后加右节点
}
else //pop说明没有加入,就要从栈中取出一个为其加入右子节点 如果下一轮还是pop说明上一个pop的结点无右子结点
{
fa=s.top();
s.pop(); //首元素
left=0; //下一轮加入的是右子结点
}
}
return root;
}
int ans[36],pos=0; //存答案
void PostOrderTraversal(int bt) //后序遍历
{
if(bt)
{
PostOrderTraversal(tn[bt].left);
PostOrderTraversal(tn[bt].right);
ans[++pos]=bt;
}
}
int main()
{
int r=BuildTree();
PostOrderTraversal(r);
for(int i=1;i<pos;i++) //答案输出,最后无空格
cout<<ans[i]<<" ";
cout<<ans[pos];
return 0;
}
方法二
输入中序遍历时对栈的操作中,push的顺序就是先序遍历的结果,而pop的顺序是中序遍历的结果
可以根据先序遍历的结果和中序遍历的结果推出后序遍历的结果
先序遍历的首元素就是根,然后再中序遍历的数列中找到根的位置,其左边的就是左子树,右边的就是右子树,然后分别对左子树和右子树进行之前的操作。
代码
#include<iostream>
#include<stack>
using namespace std;
int pre[36],in[36],post[36];
void work(int prel,int inl,int postl,int n)
{
if(n==0)
return;
if(n==1)
{
post[postl]=pre[prel];
return;
}
int root=pre[prel],i;
post[postl+n-1]=root;
for(i=0;i<=n;i++) //此时n为此次判断的数列长度 不是整个的长度 条件不要写成inl+i<=n 条件可以不写
if(in[inl+i]==root)
break;
work(prel+1,inl,postl,i);
work(prel+i+1,inl+i+1,postl+i,n-i-1);
}
int main()
{
int n,x,posp=0,posi=0;
cin>>n;
stack<int>s;
string str;
for(int i=1;i<=2*n;i++)
{
cin>>str;
if(str[1]=='u')
{
cin>>x;
s.push(x);
pre[++posp]=x;
}
else
{
x=s.top();
s.pop();
in[++posi]=x;
}
}
work(1,1,1,n);
for(int i=1;i<n;i++)
cout<<post[i]<<" ";
cout<<post[n];
return 0;
}