二叉树的遍历
来源:互联网 发布:过期域名抢注平台 编辑:程序博客网 时间:2024/05/16 13:38
void preorder(node_t *r){if(!r)return ;cout<<r->val<<' ';preorder(r->lc);preorder(r->rc);}void preorder_nonrecursive(node_t *r){cout<<"preorder_nonrecursive"<<endl;if(!r)return;stack<node_t *> st;st.push(r);while(!st.empty()){node_t *cur = st.top();cout<<cur->val<<' ';st.pop();if(cur->rc){st.push(cur->rc);}if(cur->lc){st.push(cur->lc);}}cout<<endl;}void inorder(node_t *r){if(!r)return;inorder(r->lc);cout<<r->val<<' ';inorder(r->rc);}void inorder_nonrecursive(node_t *r){cout<<"inorder_nonrecursive"<<endl;if(!r)return;stack<node_t *> st;bool flag = true;st.push(r);while(!st.empty()){node_t *cur = st.top();if(!cur->lc || !flag){st.pop();cout<<cur->val<<' ';if(cur->rc){st.push(cur->rc);flag = true;}else {flag = false;}}else {st.push(cur->lc);}}cout<<endl;}void postorder_nonrecursive(node_t *r){ if(!r)return; stack<node_t *> st; node_t *pre = r; st.push(r); while(!st.empty()){ node_t *cur = st.top(); if(!cur->lc && !cur->rc){ cout<<cur->val<<' '; pre = cur; st.pop(); }else if(pre == cur->lc || pre == cur->rc){ cout<<cur->val<<' '; pre = cur; st.pop(); }else { if(cur->rc) st.push(cur->rc); if(cur->lc) st.push(cur->lc); } }}
二叉树的先序、中序、后序遍历,递归实现与非递归实现版本。
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 二叉树的遍历
- 被叼了
- POJ 1328 Radar Installation
- Android系统中的广播(Broadcast)机制简要介绍和学习计划
- 关于传统AOI策略的一点改进口水
- 立波软件管家:方便管理安卓手机应用、应用搬家、快速摇摇卸载应用、应用备份(souapp.com搜应用网推荐)
- 二叉树的遍历
- Memcached进程自动重启脚本
- 单利模式
- 批量修改某个目录下的文件内容
- hdu 4451
- HowTo: Export C++ classes from a DLL
- win7 wif热点
- 静态成员&静态方法
- linux命令-du