二叉树非递归三种遍历
2017-01-30 21:31
232 查看
非递归先序
非递归中序
非递归后序
void nicePreOrder(treeNode* s)//非递归先序 { if (s == NULL) { return; } stack<treeNode*> st; while (s != NULL || !st.empty()) { while (s != NULL) { cout << s->data << " "; st.push(s); s = s->left; } treeNode* tmp = st.top(); st.pop(); s = tmp->right; } }
非递归中序
void niceInOrder(treeNode* s)//非递归中序 { if (s == NULL) { return; } stack<treeNode*> st; while (!st.empty() || s != NULL) { while (s != NULL) { st.push(s); s = s->left; } treeNode* tmp = st.top(); st.pop(); cout << tmp->data << " "; s = tmp->right; } }
非递归后序
void nicePastOrder(treeNode* s)//非递归后序遍历 //左右中 { if (s == NULL) { return; } stack<treeNode*> st; treeNode* tag = NULL; while (s != NULL || !st.empty()) { while (s != NULL) { st.push(s); s = s->left; } treeNode* tmp = st.top(); st.pop(); if (tmp->right == NULL || tmp->right == tag) { cout << tmp->data << " "; tag = tmp; tmp = NULL; } else//发现该结点右边还有未访问的结点 { st.push(tmp); s = tmp->right; } } }
相关文章推荐
- 二叉树的三种遍历的非递归实现
- java 二叉树三种遍历(递归非递归)
- 二叉树三种遍历的非递归思路(JAVASCRIPT)
- 二叉树的三种遍历(非递归)
- 二叉树的创建、相关操作、递归和非递归式实现三种遍历
- 递归实现二叉树的三种遍历
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 数据结构学习之-二叉树的三种递归遍历C++实现及相关应用
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- java实现二叉树的创建及三种递归遍历
- 二叉树的三种遍历方法(递归和非递归)(转载)
- 二叉树三种遍历方式的递归和循环实现
- 二叉树的三种遍历方法(递归和非递归)
- 二叉树的三种遍历方法(递归)
- java使用递归,非递归方式实现二叉树的三种常见遍历方式
- 二叉树的三种遍历的非递归实现
- 二叉树的三种遍历(递归+非递归)
- 二叉树三种遍历方式 递归与非递归实现
- 递归实现二叉树的三种遍历
- 二叉树三种遍历的非递归形式