二叉树遍历方式的转化(递归写法)
来源:互联网 发布:sql语句美化小工具 编辑:程序博客网 时间:2024/06/05 02:15
大家都知道二叉树已知中序遍历和后序遍历可以求前序遍历
已知前序遍历和中序遍历可以求后序遍历,那么现在实现一下
想法基本是利用递归,先找到根节点的位置,然后利用左右子树递归
前序遍历+中序遍历 求 后序遍历:
void dfs(char *inorder,char *preorder,int len){if(len == 0) return ;int rootindex = 0;while(rootindex<len){if(inorder[rootindex] == preorder[1])break;
rootindex++;}dfs(inorder,preorder+1,rootindex);dfs(inorder+rootindex+1,preorder+rootindex+1,len-1-rootindex);printf("%c\n",*inorder );}
中序遍历+后序遍历-->先序遍历
void dfs(char *inorder,char*preorder,int len){if(len == 0) return ;int rootindex = 0;printf("%c\n",postorder[len-1]);while(rootindex<len){if(inorder[rootindex]==postorder[len-1])break;rootindex++;}dfs(inorder,postorder,rootindex);dfs(inorder+rootindex+1,postorder+rootindex,len-1-rootindex);}
0 1
- 二叉树遍历方式的转化(递归写法)
- 二叉树的遍历方式(递归)
- 二叉树的遍历方式(递归)
- 二叉树遍历的非递归写法(伪代码)
- 二叉树的三种非递归遍历方式。
- 二叉树遍历方式之间的关系(递归)
- 二叉树的创建即遍历(递归方式)
- (C++)二叉树的建立与递归方式遍历
- 二叉树的三种遍历方式(递归、非递归和Morris遍历)
- 二叉树遍历非递归写法
- 四种遍历二叉树的方式(递归,非递归)
- 二叉树的前中后序遍历,递归和非递归方式
- 二叉树的遍历(递归遍历)
- C++实现——二叉树的四种遍历(非递归写法)
- 树及树的遍历(二)-----二叉树前序、中序、后序遍历非递归写法的透彻解析
- 二叉树的两种遍历方式[递归遍历][非递归遍历]
- 数据结构--树:二叉树的遍历方式(递归和非递归)
- 二叉树的四种遍历方式(非递归和递归)
- SIGCHLD信号处理
- canvas
- 使用python读取大文件
- 转方阵
- 【 UITextField】- 文本框
- 二叉树遍历方式的转化(递归写法)
- PYTHON学习之路一:WEB开发
- python中range()函数的用法--转载
- [译]MVC or MVP Pattern – Whats the difference?
- 记一个JSON解析,客户端,服务端
- Mac版虚拟机怎么安装win7系统(详细教程)
- Ubuntu r8168模块编译出错问题的解决办法
- 以Java后端为主,同时能够胜任前端、后端、测试、运维、DBA岗位
- (HDU 2859) Phalanx (求最大对称子矩阵) dp