二叉树非递归先序遍历、中序遍历、后序遍历
来源:互联网 发布:做淘宝要囤货吗 编辑:程序博客网 时间:2024/05/21 06:17
#include "stdafx.h"#include <stack>struct bitreeNode{int value;bitreeNode * left;bitreeNode * right;};//先序int bitreePreOrder(bitreeNode * root){stack <bitreeNode *> st;bitreeNode * node = root;do{while(root){printf("%d,", root->value);st.push(root);root = root->left;}if(st.size() == 0)break;root = (bitreeNode *)st.top();st.pop();root = root->right;}while(1);return 0;}//中序 int bitreeInOrder(bitreeNode * root){stack <bitreeNode *> st;bitreeNode * node = root;do{while(root){st.push(root);root = root->left;}if(st.size() == 0)break;root = (bitreeNode *)st.top();st.pop();printf("%d", root->value);root = root->right;}while(1);return 0;}//后序struct BadIdea{bitreeNode * node;bool first;};int bitreePostOrder(bitreeNode * root){stack <BadIdea> st;BadIdea idea;BadIdea node;do{while(root){idea.node = root;idea.first = true;st.push(idea);root = root->left;}if(st.size() == 0)break;node = (BadIdea)st.top();st.pop();if(node.first){node.first = false;root = node.node->right;st.push(node);}else{printf("%d", node.node->value);root = NULL;}}while(1);return 0;}#define BITREE_SIZE 10int _tmain(int argc, _TCHAR* argv[]){bitreeNode * root = (bitreeNode *)malloc(sizeof(bitreeNode) * BITREE_SIZE);for(int i=0; i<BITREE_SIZE; i++){root[i].value = i;if((i * 2 +1) < BITREE_SIZE)root[i].left = &(root[i*2+1]);elseroot[i].left = NULL;if((i * 2 + 2) < BITREE_SIZE)root[i].right = &(root[i*2 + 2]);elseroot[i].right = NULL;}bitreePreOrder(root);bitreeInOrder(root);bitreePostOrder(root);return 0;}
0 0
- 二叉树 非递归 先序遍历 中序遍历 后序遍历 层次遍历
- 二叉树非递归先序遍历、中序遍历、后序遍历
- 二叉树遍历(先序遍历、中序遍历、后序遍历)——递归方法和非递归方法
- 二叉树的先序、中序、后序递归遍历和非递归遍历
- 二叉树的遍历(2)--先序遍历,中序遍历,后序遍历(非递归)
- 二叉树的后序,先序,中序遍历的非递归遍历
- 先序遍历中序遍历后序遍历确定一棵二叉树(递归、非递归)
- 二叉树的先序遍历、中序遍历以及后序遍历(递归以及非递归方式)
- 递归法遍历二叉树 (先序遍历 中序遍历 后序遍历)
- 非递归先序遍历二叉树、非递归中序遍历二叉树、非递归后序遍历二叉树
- 非递归先序遍历二叉树
- 非递归先序遍历二叉树
- 先序遍历二叉树 [非递归]
- 非递归先序遍历二叉树
- 二叉树非递归先序遍历
- 二叉树非递归先序遍历
- 非递归先序遍历二叉树
- java建立二叉树,递归/非递归先序遍历,递归/非递归中序遍历,层次遍历
- 由锹撞姓萌壮妇祷阎狈剖淤豪嚎氏
- android webview 远程js访问本地方法
- 百度地图iOS开发 HelloWorld 流程,警告及解决办法
- Struts2 restful配置及使用
- Android之SurfaceView实现视频播放
- 二叉树非递归先序遍历、中序遍历、后序遍历
- virtualbox无法安装VBoxLinuxAdditions.run
- Linux网络协议栈基础
- Java Swing:为JMenuBar(JToolBar)设置背景图片
- android 通过getViewTypeCount 和 getItemViewType实现item加载不同布局
- iOS - Xcode升级到5.1& iOS升级到iOS7.1问题:Undefined symbols for architecture x86_64
- 基数排序组合桶式排序
- 数据结构之哈希表
- Tomcat性能监控工具Probe Quick Start