非递归后序遍历二叉树版本一
如果第一遍看不懂不要紧,请手动模拟体会。这个方法是设置最近访问节点,另解参考博主博文版本二
func(Tree T){
if(T==NULL){
printf("树空");
return
}
Stact S;
TreeNode r=NULL;//用于标记是否是从右子树返回的,读者手动模拟一遍就知道了
while(T!=NULL||!IsEmpty(S)){
if(T!=NULL){
push(S,T);
T=T->lchild;
}
else{
GetTop(S,T); //注意此处不是pop
if(T->rchild!=null&&T->rchild!=r){
T=T->rchild;
push(S,T);
T=T->lchild;
}
else{
pop(S,T);
visit(T);
r=T;//记住最近访问的结点
T=NULL;
}
}
}
}
您可能感兴趣的- 非递归先序遍历二叉树https://www.cnblogs.com/Coeus-P/p/9353186.html
- 非递归后序遍历二叉树版本二https://www.cnblogs.com/Coeus-P/p/9354754.html
- 递归算法--二叉树宽度https://www.cnblogs.com/Coeus-P/p/9354671.html
- 递归算法--交换二叉树左右子树https://www.cnblogs.com/Coeus-P/p/9353568.html
- 递归算法--二叉树高度https://www.cnblogs.com/Coeus-P/p/9353528.html
- 递归算法--二叉树中叶子结点https://www.cnblogs.com/Coeus-P/p/9353505.html
- 递归算法--二叉树中度为2的结点https://www.cnblogs.com/Coeus-P/p/9353495.html
- 递归算法--二叉树中度为1的结点https://www.cnblogs.com/Coeus-P/p/9353484.html
- 非递归实现斐波那契数列https://www.cnblogs.com/Coeus-P/p/9353452.html
- 非递归后序遍历二叉树版本一https://www.cnblogs.com/Coeus-P/p/9353360.html
- 层次遍历二叉树https://www.cnblogs.com/Coeus-P/p/9353257.html
- 非递归中序遍历二叉树https://www.cnblogs.com/Coeus-P/p/9353227.html
- 非递归先序遍历二叉树https://www.cnblogs.com/Coeus-P/p/9353186.html