二叉树前序、中序和后序的非递归遍历
来源:互联网 发布:吹笛子软件安卓版 编辑:程序博客网 时间:2024/06/16 20:37
把经典问题:二叉树的非递归遍历理了理,顺便做个笔记。
核心思路就是节点的压栈和出栈。
#include <iostream>#include<stack>#include<vector>using namespace std;struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x) { left = right = nullptr; } TreeNode(int x, TreeNode* theLeft, TreeNode* theRight) : val(x), left(theLeft), right(theRight) { }};class Solution { TreeNode* root; void print(vector<int> &result, const char* str) { vector<int>::iterator it = result.begin(); cout << str; for (; it != result.end(); ++it) cout << *it << " "; cout << endl; }public: Solution(TreeNode* theRoot) : root(theRoot) { } TreeNode* getRoot() { return root; } //递归遍历 void prePrint_D(TreeNode* root) { if (root) { cout << root->val << " "; prePrint_D(root->left); prePrint_D(root->right); } } void midPrint_D(TreeNode* root) { if (root) { midPrint_D(root->left); cout << root->val << " "; midPrint_D(root->right); } } void postPrint_D(TreeNode* root) { if (root) { postPrint_D(root->left); postPrint_D(root->right); cout << root->val << " "; } } //非递归遍历 void prePrint() { if (!root) return; stack<TreeNode*> nodeStack; vector<int> result; TreeNode* cNode = root; while (cNode || !nodeStack.empty()) { result.push_back(cNode->val); nodeStack.push(cNode); cNode = cNode->left; while (!cNode && !nodeStack.empty()) { cNode = nodeStack.top(); nodeStack.pop(); cNode = cNode->right; } } print(result, "preOrder: "); } void midPrint() { if (!root) return; stack<TreeNode*> nodeStack; vector<int> result; TreeNode* cNode = root; while (cNode || !nodeStack.empty()) { if (cNode->left) { nodeStack.push(cNode); cNode = cNode->left; } else { result.push_back(cNode->val); cNode = cNode->right; while (!cNode && !nodeStack.empty()) { cNode = nodeStack.top(); nodeStack.pop(); result.push_back(cNode->val); cNode = cNode->right; } } } print(result, "midOrder: "); } void postPrint() { if (!root) return; stack<TreeNode*> nodeStack; vector<int> result; TreeNode* cNode = root; nodeStack.push(cNode); while (!nodeStack.empty()) { cNode = nodeStack.top(); if ((!cNode->left && !cNode->right) || //左右子树为空 (!result.empty() && cNode->right && cNode->right->val == result[result.size() - 1]) //有右子树,右子树刚遍历完 || (!result.empty() && cNode->left && cNode->left->val == result[result.size() - 1])) { //没有右子树,但左子树刚遍历完 //此时cNode节点的子树已全部遍历完,输出该节点,并从栈里删除 result.push_back(cNode->val); nodeStack.pop(); } else { //将左右子树压入栈,由于遍历顺序是先左后右,所以入栈顺序是先右后左 if (cNode->right) nodeStack.push(cNode->right); if (cNode->left) nodeStack.push(cNode->left); } } print(result, "postOrder: "); }};int main(){ TreeNode* p1 = new TreeNode(1), *p2 = p1->left = new TreeNode(2), *p3 = p1->right = new TreeNode(3), *p4 = p2->left = new TreeNode(4), *p5 = p2->right = new TreeNode(5), *p6 = p3->left = new TreeNode(6), *p7 = p3->right = new TreeNode(7), *p8 = p4->left = new TreeNode(8), *p9 = p4->right = new TreeNode(9), *p10 = p5->left = new TreeNode(10), *p11 = p5->right = new TreeNode(11), *root = p1; Solution S(root); cout << "递归:" << endl; S.prePrint(); S.midPrint(); S.postPrint(); cout << "递归:" << endl; cout << "preOrder: "; S.prePrint_D(S.getRoot()); cout << endl; cout << "midOrder: "; S.midPrint_D(S.getRoot()); cout << endl; cout << "postOrder: "; S.postPrint_D(S.getRoot()); cout << endl; //...释放二叉树内存代码段... return 0;}
输出:
递归:preOrder: 1 2 4 8 9 5 10 11 3 6 7 midOrder: 8 4 9 2 10 5 11 1 6 3 7 postOrder: 8 9 4 10 11 5 2 6 7 3 1 递归:preOrder: 1 2 4 8 9 5 10 11 3 6 7 midOrder: 8 4 9 2 10 5 11 1 6 3 7 postOrder: 8 9 4 10 11 5 2 6 7 3 1
阅读全文
0 0
- c/c++实现二叉树前序,中序和后序的递归和非递归遍历
- 二叉树前序,中序,后序的遍历【递归(借用栈实现)和非递归】
- 【二叉树】 前序、中序和后序的递归遍历与非递归遍历
- 二叉树的先序、中序、后序递归遍历和非递归遍历
- 二叉树先序遍历,中序遍历和后序遍历的非递归实现
- 二叉树前序、中序和后序的非递归遍历
- 二叉树的先序、中序、后序遍历的递归和非递归实现
- 二叉树前序遍历、中序遍历、后序遍历的非递归实现
- 二叉树的前序遍历、中序遍历和后序遍历的递归和非递归算法
- 二叉树的创建和先序,中序,后序,递归,非递归遍历
- 数据结构:二叉树的前序,中序,后序遍历(递归和非递归)
- 二叉树的递归和非递归遍历(前序、中序、后序)
- 二叉树的前序,中序,后序遍历。用递归和非递归实现
- 二叉树的递归和非递归前、中、后序遍历
- 二叉树先序,中序,后序的非递归遍历
- 二叉树的非递归【前/中/后 序遍历】
- 二叉树非递归后序遍历(非递归遍历中最麻烦的一个)
- 二叉树前序、中序、后序遍历非递归写法
- D
- ArangoDB入门教程(二)web端介绍
- 动态代理与AOP(4)
- ORACLE DataGuard 主备库角色切换
- jquery判断checkbox是否选中
- 二叉树前序、中序和后序的非递归遍历
- singleLine过期,淘汰后的替代方法
- 初识Django-模版文件templates
- POJ2279 爆内存做法 (MLE) DP写法(练手
- [笔记] CentOS 7 部署 Kubernetes 集群自动化脚本部分
- mysql 时间间隔
- Android AOP面向切面编程详解
- java获取当年第一天
- HTTP与HTTPS的区别