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、从二叉树中序遍历非递归算法的进出栈顺序可以直接构造该二叉树
共6种情形
1、第一次Push(存根结点)
2、连续两次Push(本次Push的结点未上一次Push的结点的左孩子)
3、上一次Push,本次Pop(上一次Push的结点无左孩子)
4、上一次Pop,本次Push(本次push的结点为上次Pop的结点的右孩子)
5、连续两次Pop(不处理)
6、最后一次Pop(最后一次Pop的结点没有右孩子)
2、递归后序遍历输出结果
3、本题其它方法(由二叉树中序遍历非递归算法的进栈顺序可以得到该二叉树的前序遍历的结果。再按照前序遍历和中序遍历去推出后序遍历的结果)
#include<iostream> #include<vector> #include<string> using namespace std; #define Max_Node 32 typedef struct node { int left; int right; }Node; typedef struct stack//静态栈 { int Data[Max_Node]; int Top; }Stack; void Initialize_Stack(Stack& S) { S.Top=0; } void Push(Stack& S,int data) { S.Data[S.Top++]=data; } int Pop(Stack& S) { return S.Data[--(S.Top)]; } void PostOrderTraverse(int data,vector<Node>& Tree);//后序遍历二叉树 int main() { Stack S; Initialize_Stack(S); vector<Node> Tree(Max_Node); int N; cin>>N; int number,Pre=0,Now=0,Pre_Num=0,root=0; string str; for (int i=0; i<2*N; ++i)//接受二叉树非递归中序遍历的输入时在线处理 { cin>>str; if (str=="Push")//输入此行为Push,准备接收数字 { cin>>number; if (root==0) { root=1; root=number; } Push(S,number); Now=1;//当前行为Push操作 if (Pre==1)//上一次为Push { Tree[Pre_Num].left=number; } if (Pre==2)//上一次为Pop { Tree[Pre_Num].right=number; } Pre=Now; Pre_Num=number; }else { number=Pop(S); Now=2;//本次操作为Pop if (Pre==1) { Tree[Pre_Num].left=-1; } if (Pre==2) { Tree[Pre_Num].right=-1; } Pre=Now; Pre_Num=number; if (i==(2*N-1))//最后一个Pop结点的无右孩子 { Tree[number].right=-1; } } } Tree[Max_Node-1].left=0; PostOrderTraverse(root, Tree);//后序遍历输出结果 return 0; } void PostOrderTraverse(int data,vector<Node>& Tree) { if (Tree[data].left!=-1) { PostOrderTraverse(Tree[data].left, Tree); } if (Tree[data].right!=-1) { PostOrderTraverse(Tree[data].right, Tree); } if (Tree[Max_Node-1].left==0)//Tree最后一个元素的左孩子用作标志位,用来保证输出格式 { Tree[Max_Node-1].left=1; cout<<data; }else { cout<<' '<<data; } return; }