二叉树的前序、中序、后续遍历,递归、非递归实现
来源:互联网 发布:立项依据怎么写 知乎 编辑:程序博客网 时间:2024/04/24 10:21
前序遍历递归:
后续遍历递归
前序遍历非递归
后续遍历非递归(增加指针指向上个遍历的节点,注意栈的push顺序与前序、中序不同)
#include <vector>#include <iostream>using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};void pre_order_recursive(TreeNode* root, vector<int>& result) {if(!root) return;result.push_back(root->val);if(root->left)pre_order_recursive(root->left, result);if (root->right)pre_order_recursive(root->right, result);}/* 1 / \ 2 3/ \ / \ 4 5 6 7*/int main(){TreeNode* n1 = new TreeNode(1);TreeNode* n2 = new TreeNode(2);TreeNode* n3 = new TreeNode(3);TreeNode* n4 = new TreeNode(4);TreeNode* n5 = new TreeNode(5);TreeNode* n6 = new TreeNode(6);TreeNode* n7 = new TreeNode(7);n1->left = n2; n1->right = n3;n2->left = n4; n2->right = n5;n3->left = n6; n3->right = n7;vector<int> res;pre_order_recursive(n1, res);for(int i = 0; i < res.size(); i++){cout << res[i] << " ";}system("PAUSE");}
中序遍历递归
void in_order_recursive(TreeNode* root, vector<int>& result) {if(root->left)in_order_recursive(root->left, result);if(root)result.push_back(root->val);if(root->right)in_order_recursive(root->right, result);}
后续遍历递归
void post_order_recursive(TreeNode* root, vector<int>& result) {if(root->left)post_order_recursive(root->left, result);if(root->right)post_order_recursive(root->right, result);if(root)result.push_back(root->val);}
前序遍历非递归
void pre_order(TreeNode* root, vector<int>& result) {stack<TreeNode*> st;while(root || !st.empty()){while (root){st.push(root);result.push_back(root->val);root = root->left;}if(!st.empty()){root = st.top();st.pop();root = root->right;}}}
void in_order(TreeNode* root, vector<int>& result) {stack<TreeNode*> st;while(root || !st.empty()){while (root){st.push(root);root = root->left;}if(!st.empty()){root = st.top();result.push_back(root->val);st.pop();root = root->right;}}}
后续遍历非递归(增加指针指向上个遍历的节点,注意栈的push顺序与前序、中序不同)
void post_order(TreeNode* root, vector<int>& result) {stack<TreeNode*> st;TreeNode* pre = NULL;st.push(root);while(!st.empty()){root = st.top();if((root->left == NULL && root->right == NULL) || (pre == root->right) || (root->right == NULL && pre == root->left)){result.push_back(root->val);cout << root->val << " ";pre = root;st.pop();}else{if(root->right)st.push(root->right);if(root->left)st.push(root->left);}}}(转)另一种方式(每个节点增加访问次数标识,push顺序与之前一致)
void postOrder2(BinTree *root) //非递归后序遍历{ stack<BTNode*> s; BinTree *p=root; BTNode *temp; while(p!=NULL||!s.empty()) { while(p!=NULL) //沿左子树一直往下搜索,直至出现没有左子树的结点 { BTNode *btn=(BTNode *)malloc(sizeof(BTNode)); btn->btnode=p; btn->isFirst=true; s.push(btn); p=p->lchild; } if(!s.empty()) { temp=s.top(); s.pop(); if(temp->isFirst==true) //表示是第一次出现在栈顶 { temp->isFirst=false; s.push(temp); p=temp->btnode->rchild; } else //第二次出现在栈顶 { cout<<temp->btnode->data<<" "; p=NULL; } } } }
0 0
- 二叉树的前序、中序、后续遍历,递归、非递归实现
- leetcode | 二叉树的前序遍历、中序遍历、后续遍历的非递归实现
- 二叉树的非递归前序,中序,后续遍历实现
- 递归和非递归俩种方法实现二叉树的前序、中序、后续遍历
- Java实现树的遍历(前序、中序、后续(递归|非递归)、层次)
- 二叉树的前序遍历、中序遍历、后续遍历 (包括递归、非递归,共六种)
- 二叉树的先序、中序、后续遍历的递归和非递归实现
- 【Java语言学习】之递归、非递归前序、中序、后续遍历二叉树
- 二叉树的实现&&递归和非递归方式前序、中序、后续遍历&&发现一个节点中序遍历的下一节点
- 二叉树(一) 先序遍历、中序遍历、后续遍历、层次遍历的递归与非递归实现
- 二叉树的三种遍历递归法和非递归法(前序、中序、后续)
- 二叉树先序、中序、后续遍历(非递归实现)
- C++用非递归实现二叉树的前序排列,中序排列,后续排列
- Java 二叉树的前序、中序、后续遍历 递归和迭代实现
- 二叉树 前序遍历的非递归实现 中序遍历的非递归实现 后序遍历的非递归实现 创建二叉树
- 二叉树非递归前、中、后序遍历实现
- 递归和非递归实现二叉树的后续遍历
- 二叉树非递归遍历(前序,中序,后续遍序)
- 微信卡券开发错误自排查参考文档
- Cocos2d打包安卓APK
- 面向对象
- C语言inline函数
- 一般题模板
- 二叉树的前序、中序、后续遍历,递归、非递归实现
- AICL(Auto Input Current Limited)
- 《深入浅出struts2》--第四章,OGNL
- QListWidget实现图片缩略图形式的列表
- Day02笔记
- Extjs4 源码分析系列一 类的创建过程
- JAVA之ConcurrentHashMap源码深度分析
- ant.xml文件
- C# 属性和字段 get set