二叉树 前序,中序,后序遍历 --非递归遍历
来源:互联网 发布:电玩巴士淘宝 编辑:程序博客网 时间:2024/06/05 04:15
前序遍历:
#include <iostream>#include <stack>using namespace std;struct TreeNode { int value; TreeNode* left; TreeNode* right;};TreeNode* createNode(int value) { TreeNode* node = new TreeNode; node -> value = value; node -> left = NULL; node -> right = NULL; return node;}void BinaryTree_PreOrder_1(TreeNode* root) { //用栈记忆 if(root == NULL) return; stack<TreeNode*> st; st.push(root); while(!st.empty()) { TreeNode* node = st.top(); st.pop(); cout<<node -> value<<ends; if(node -> right) st.push(node -> right); if(node -> left) st.push(node -> left); }}void BinaryTree_PreOrder_2(TreeNode* root) { //结点增加指向父节点的指针:通过指向父节点的指针来回溯 if(root == NULL) return; stack<TreeNode*> st; while((root != NULL) || !st.empty()) { if(root != NULL) { cout<<root -> value<<ends; st.push(root); root = root -> left; } else { root = st.top(); st.pop(); root = root -> right; } }}int main() { TreeNode* root = new TreeNode; root -> value = 10; root -> left = createNode(5); root -> right = createNode(12); root -> left -> left = createNode(4); root -> left -> right = createNode(7); BinaryTree_PreOrder_1(root); cout<<endl; BinaryTree_PreOrder_2(root);}
中序遍历:
#include <iostream>#include <stack>using namespace std;struct TreeNode { int value; TreeNode* left; TreeNode* right;};TreeNode* createNode(int value) { TreeNode* node = new TreeNode; node -> value = value; node -> left = NULL; node -> right = NULL; return node;}void BinaryTree_InOrder(TreeNode* root) { if(root == NULL) return; stack<TreeNode*> st; while((root != NULL) || !st.empty()) { if(root != NULL) { //在前序遍历第二种方法中修改了下 st.push(root); root = root -> left; } else { root = st.top(); st.pop(); cout<<root -> value<<ends; root = root -> right; } }}int main() { TreeNode* root = new TreeNode; root -> value = 10; root -> left = createNode(5); root -> right = createNode(12); root -> left -> left = createNode(4); root -> left -> right = createNode(7); BinaryTree_PreOrder_1(root); cout<<endl; BinaryTree_InOrder(root);}
后序遍历:
#include <iostream>#include <stack>using namespace std;struct TreeNode { int value; TreeNode* left; TreeNode* right;};TreeNode* createNode(int value) { TreeNode* node = new TreeNode; node -> value = value; node -> left = NULL; node -> right = NULL; return node;}void BinaryTree_PostOrder_1(TreeNode* root) { // 后序遍历的非递归 if(root == NULL) return; stack<TreeNode*> st; TreeNode* curr = root; // 指向当前要检查的节点 TreeNode* PreVis = NULL; // 指向前一个被访问的节点 while(curr != NULL || !st.empty()) { // 栈空时结束 while(curr != NULL) { // 一直向左走直到为空 st.push(curr); curr = curr -> left; } curr = st.top(); // 当前节点的右孩子如果为空或者已经被访问,则访问当前节点 if(curr -> right == NULL || curr -> right == PreVis) { cout<<curr -> value<<ends; PreVis = curr; st.pop(); curr = NULL; } else { // 否则访问右孩子 curr = curr -> right; } }}void BinaryTree_PostOrder_2(TreeNode* root) { // 后序遍历的非递归 双栈法 stack<TreeNode*> s1, s2; TreeNode* curr; // 指向当前要检查的节点 s1.push(root); while(!s1.empty()) { // 栈空时结束 curr = s1.top(); s1.pop(); s2.push(curr); if(curr -> left) { s1.push(curr -> left); } if(curr -> right) { s1.push(curr -> right); } } while(!s2.empty()) { cout<<s2.top() -> value<<ends; s2.pop(); }}int main() { TreeNode* root = new TreeNode; root -> value = 1; root -> left = createNode(2); root -> right = createNode(3); root -> left -> left = createNode(4); root -> left -> right = createNode(5); root -> right -> left = createNode(6); root -> right -> right = createNode(7); BinaryTree_PostOrder_1(root); cout<<endl; BinaryTree_PostOrder_2(root);}
0 0
- 二叉树非递归前、中、后序遍历
- 二叉树非递归前、中、后序遍历实现
- 二叉树的非递归【前/中/后 序遍历】
- 非递归前,中,后序遍历二叉树
- 非递归实现二叉树的后序遍历、前序遍历、中序遍历
- 【二叉树】 前序、中序和后序的递归遍历与非递归遍历
- 前、中、后、序递归遍历二叉树以及非递归遍历
- C# 二叉树的非递归中序 前序 后序遍历 层序遍历
- 二叉树 前序,中序,后序遍历 --非递归遍历
- 二叉树的非递归遍历以及层次遍历(前序、中序、后序)
- 二叉树的前序遍历、中序遍历和后序遍历的递归和非递归算法
- 前,中,后序遍历二叉树 (递归 && 非递归的栈 && 非递归非栈的线索二叉树)
- 二叉树的非递归前序、后序遍历
- 二叉树 前序遍历的非递归实现 中序遍历的非递归实现 后序遍历的非递归实现 创建二叉树
- 接前-后序遍历二叉树(非递归)
- 二叉树的前序、中序、后序遍历,非递归前、中、后序遍历
- 二叉树的创建,遍历(前序,中序,后序)-递归 非递归
- 二叉树的前序、中序、后序遍历 递归非递归实现
- linux系统编程-学习笔记5-opendir/readdir/getcwd
- Median of two sorted array
- linux下一般程序运行时查找动态库的顺序
- 全新版大学英语四综合教程答案
- B-树和B+树的应用:数据搜索和数据库索引
- 二叉树 前序,中序,后序遍历 --非递归遍历
- day42,page70
- [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(上)
- HDU 2647 Reward 分层拓扑排序
- 田忌赛马(较难贪心)
- csv helper
- 内存
- jsp+mysql开发网站留言板源代码下载
- LeetCode: Unique Binary Search Trees II