二叉树前序、中序、后序遍历
来源:互联网 发布:完全卸载sqlserver 编辑:程序博客网 时间:2024/06/06 20:53
三种遍历
遍历即将树的所有结点访问且仅访问一次。按照根节点位置的不同分为前序遍历,中序遍历,后序遍历。
前序遍历:根节点->左子树->右子树
中序遍历:左子树->根节点->右子树
后序遍历:左子树->右子树->根节点
前序遍历:abdefgc
中序遍历:debgfac
后序遍历:edgfbca
递归实现:
前序递归实现:
void preorder(bintree t){ if(t){ printf("%c ",t->data); preorder(t->lchild); preorder(t->rchild); }}中序和后序遍历只是把打印的位置改变下。
非递归实现
非递归算法参考:http://blog.csdn.net/zhangxiangdavaid/article/details/37115355
中序:
//中序遍历void InOrderWithoutRecursion2(BTNode* root){//空树if (root == NULL)return;//树非空BTNode* p = root;stack<BTNode*> s;while (!s.empty() || p){if (p){s.push(p);p = p->lchild;}else{p = s.top();s.pop();cout << setw(4) << p->data;p = p->rchild;}}}
前序:
//前序遍历void PreOrderWithoutRecursion2(BTNode* root){if (root == NULL)return;BTNode* p = root;stack<BTNode*> s;while (!s.empty() || p){if (p){cout << setw(4) << p->data;s.push(p);p = p->lchild;}else{p = s.top();s.pop();p = p->rchild;}}cout << endl;}
查找、统计个数、比较、求深度的递归实现
参考:http://blog.csdn.net/fansongy/article/details/6798278/
阅读全文
1 0
- 二叉树前序遍历、中序遍历和后序遍历
- 二叉树前序、中序、后序遍历
- 二叉树前序、中序、后序遍历
- 二叉树前序,中序,后序遍历
- 二叉树前序、中序、后序遍历
- 二叉树前序、中序、后序遍历
- 二叉树先序遍历、中序遍历、后序遍历
- 二叉树先序遍历,中序遍历,后序遍历
- 二叉树先序、中序、后序遍历
- 4. 二叉树前序、中序、后序递归遍历算法,二叉树前序非递归遍历算法
- 二叉树前序,中序,后序遍历详解
- 二叉树前序,中序,后序遍历详解
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 二叉树前序、中序、后序遍历相互求法
- 百度编辑器ueditor,怎么配置前端文件ueditor.config.js
- iPad开发
- JS继承的实现方式
- Struts2 使用案例(操作流程)
- spring boot(六):如何优雅的使用mybatis
- 二叉树前序、中序、后序遍历
- ReactNative实现图集功能
- Android零基础入门第85节:Fragment使用起来非常简单
- lesson33 三种循环语句
- cf 6789 this one ttt
- 固态硬盘的了解
- tensorflow 的tensorboard使用
- 用redis实现支持优先级的消息队列
- SSH 端口转发实战