基于visual Studio2013解决面试题之0401非递归遍历二叉树
来源:互联网 发布:comic studio mac破解 编辑:程序博客网 时间:2024/05/16 06:54
题目
解决代码及点评
/*非递归实现中序遍历二叉树中序遍历概念:先访问左子树,然后再访问根节点,然后再访问右子树用递归的方法非常简单,理解思想几行代码,但是不用递归该如何实现?不用递归,则需要用栈来保存现场,要遍历左子树之前,将根节点压栈再访问左子树,当该根节点要被弹出来时,说明左子树已经遍历完毕以下代码只注释inOrder,其他类推即可*/#include <iostream>#include <stack>using namespace std;typedef struct BiTNode{int nValue;struct BiTNode *pLChild;struct BiTNode *pRChild;}BiTNode, *PBiTree;PBiTree Create(){int nValue;PBiTree pRoot;scanf_s("%d", &nValue);if (nValue == 0){pRoot = NULL;}else{pRoot = (PBiTree)malloc(sizeof(BiTNode));if (NULL == pRoot){printf("分配内存失败!\n");}else{pRoot->nValue = nValue;printf("请输入%d结点的左子结点:", pRoot->nValue);pRoot->pLChild = Create();printf("请输入%d结点的右子结点:", pRoot->nValue);pRoot->pRChild = Create();}}return pRoot;}void Visit(PBiTree p){printf("%d ", p->nValue);}void PreOrder(PBiTree pRoot) {stack<PBiTree> pStack;PBiTree pCur = pRoot;while (pStack.size()>0||pCur!=NULL){while (pCur!=NULL)//遍历到左边最下边{Visit(pCur); //和中序相似,就是这一句位置不同pStack.push(pCur);pCur=pCur->pLChild;}if(pStack.size()>0){pCur=pStack.top();pStack.pop();pCur=pCur->pRChild;}}}//中序遍历void InOrder(PBiTree pRoot) {stack<PBiTree> pStack;PBiTree pCur = pRoot;while (pStack.size()>0 || pCur!=NULL){// 通过这层循环,找到最左下边节点while (pCur!=NULL)//遍历到左边最下边{// 如果左儿子不是空,则压栈pStack.push(pCur);pCur=pCur->pLChild;}// 将最左下的节点弹出并访问,然后将pCur指向最左下节点的右儿子,继续遍历if(pStack.size()>0){// 访问栈顶的元素pCur=pStack.top();Visit(pCur);// 弹出栈顶pStack.pop();// 然后访问右子树pCur=pCur->pRChild;}}}void PostOrder(PBiTree pRoot){stack<PBiTree> pStack;PBiTree pCur = pRoot;while (pCur!=NULL)//遍历到左边最下边{pStack.push(pCur);pCur=pCur->pLChild;}while(pStack.size()>0){PBiTree pLastVisit = pCur; //判断当前该访问左节点还是右节点pCur=pStack.top();pStack.pop();//pStack.pop();if (pCur->pRChild==NULL||pCur->pRChild==pLastVisit){Visit(pCur);}else if (pCur->pLChild==pLastVisit){pStack.push(pCur);pCur=pCur->pRChild;pStack.push(pCur);while (pCur!=NULL){if (pCur->pLChild!=NULL){pStack.push(pCur->pLChild);}pCur=pCur->pLChild;}}}}int main(){printf("请输入根结点的值:");PBiTree pRoot = Create();printf("前序遍历:");PreOrder(pRoot);cout<<endl;printf("中序遍历:");InOrder(pRoot);cout<<endl;printf("后序遍历:");PostOrder(pRoot);cout<<endl;system("pause");return 0;}
代码下载及其运行
代码下载地址:http://download.csdn.net/detail/yincheng01/6704519
解压密码:c.itcast.cn
下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:
1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”
2)在下拉框中选择相应项目,项目名和博客编号一致
3)点击“本地Windows调试器”运行
程序运行结果
0 0
- 基于visual Studio2013解决面试题之0401非递归遍历二叉树
- 基于visual Studio2013解决面试题之0208二叉搜索树后序遍历序列
- 基于visual Studio2013解决面试题之0201二叉树转链表
- 基于visual Studio2013解决面试题之0304镜像二叉树
- 基于visual Studio2013解决面试题之0305广度优先搜索二叉树
- 基于visual Studio2013解决面试题之0601二叉树深度
- 基于visual Studio2013解决面试题之0203栈实现
- 基于visual Studio2013解决面试题之0205查找路径
- 基于visual Studio2013解决面试题之0207单词翻转
- 基于visual Studio2013解决面试题之0202上下排
- 基于visual Studio2013解决面试题之0301累加
- 基于visual Studio2013解决面试题之0303数组求和
- 基于visual Studio2013解决面试题之0308Fibonacci数列
- 基于visual Studio2013解决面试题之0403串联字符串
- 基于visual Studio2013解决面试题之0407数组差
- 基于visual Studio2013解决面试题之0501上台阶
- 基于visual Studio2013解决面试题之0504单链表逆序
- 基于visual Studio2013解决面试题之0602全排列
- 数据持久化之FMDB简单使用
- 使用python实现标记图像感兴区域
- oracle只装客户端远程连接数据库
- Unity 阴影
- 虚拟键盘代码
- 基于visual Studio2013解决面试题之0401非递归遍历二叉树
- loadrunner-27796错误寻求解决办法
- 复习————简单函数输出最大值
- eclipse中部署编写extjs代码时候的检测插件spket
- sharepoint 2010 列表数据分页控件介绍 pagination UserControl
- HTC EVO 3D G17解锁s-off刷机图文教程
- python下的MySQLdb使用
- 东软实训推荐面试问题16:我们公司今年计划一般不招女学生
- Tomcat7.0启动