/**
二叉树后序遍历非递归算法(有问题)
分析:
a(flag=1),只遍历完左子树,右子树尚未遍历,则该结点不出栈,利用栈顶结点找到它的右子树,准备遍历
b(flag=2),遍历完右子树,将该结点出栈,并访问它
*/
struct BiNode{
char data;
BiNode *lchild,*rchild;
};
struct Element{
BiNode *bt;
int flag;
};
void postOrder2(BiNode *bn){
int top=-1;
Element s[20];///假定不会发生上溢
while(bn!=NULL||top!=-1){///两个条件都不成立才退出循环
while(bn!=NULL){
top++;
s[top].bt=bn;
s[top].flag=1;///结点连同标志位一起入栈
bn=bn->lchild;
}
while(top!=-1&&s[top].flag==2){
bn=s[top--].bt;
cout<data;
}
if(top!=-1){
s[top].flag=2;
bn=s[top].bt->rchild;
}
}
}
运行后神秘问题 你把错误贴出来啊