C++之根据组合遍历顺序生成二叉树(25)---《那些奇怪的算法》
来源:互联网 发布:java 反射 私有属性 编辑:程序博客网 时间:2024/06/15 22:18
在数据结构中,我们经常需要根据不同的组合遍历顺序确定一个二叉树,并生成它,现在就来整理一下思路,这儿以中序遍历和后序遍历为例!
如上图所示,我们先由于后序遍历找到根结点,然后在中序遍历的序列中找到对应的根结点,此时可以将中序遍历和后续遍历中左右子树的位置确定,然后递归进行划分,注意递归的结束条件的设置!
实现代码如下:
1、给定中序遍历和后序遍历实现:
1)递归结束条件为:i_l>i_r,注意调用时候的参数设置!
class Solution {public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { if(inorder.size()==0)return NULL; TreeNode* root=buildTree_(inorder,0,inorder.size()-1,postorder,0,postorder.size()-1); return root; } TreeNode* buildTree_(vector<int>& inorder,int i_l,int i_r,vector<int>& postorder,int p_l,int p_r){ if(i_l>i_r) return NULL; TreeNode* root=new TreeNode(postorder[p_r]); int count=0; for(int i=i_l;i<i_r;i++){ if(inorder[i]==postorder[p_r]) break; count++; } root->left=buildTree_(inorder,i_l,i_l+count-1,postorder,p_l,p_l+count-1); root->right=buildTree_(inorder,i_l+count+1,i_r,postorder,p_l+count,p_r-1); return root; }};
2)递归结束条件为:i_l==i_r,注意调用的参数设置!
class Solution {public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { if(inorder.size()==0)return NULL; TreeNode* root=buildTree_(inorder,0,inorder.size(),postorder,0,postorder.size()); return root; } TreeNode* buildTree_(vector<int>& inorder,int i_l,int i_r,vector<int>& postorder,int p_l,int p_r){ if(i_l==i_r) return NULL; TreeNode* root=new TreeNode(postorder[p_r-1]); int count=0; for(int i=i_l;i<i_r;i++){ if(inorder[i]==postorder[p_r-1]) break; count++; } root->left=buildTree_(inorder,i_l,i_l+count,postorder,p_l,p_l+count); root->right=buildTree_(inorder,i_l+count+1,i_r,postorder,p_l+count,p_r-1); return root; }};
2、给定先序遍历和中序遍历实现:
1)递归条件为i_l>i_r时:
class Solution {public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { if(preorder.empty()) return NULL; TreeNode* root=buildTree_(preorder,0,preorder.size()-1,inorder,0,inorder.size()-1); return root; } TreeNode* buildTree_(vector<int>& preorder,int p_l,int p_r,vector<int>& inorder,int i_l,int i_r){ if(i_l>i_r) return NULL; TreeNode* root=new TreeNode(preorder[p_l]); int count=0; for(int i=i_l;i<=i_r;i++){ if(inorder[i]==preorder[p_l]) break; count++; } root->left=buildTree_(preorder,p_l+1,p_l+count,inorder,i_l,i_l+count-1); root->right=buildTree_(preorder,p_l+count+1,p_r,inorder,i_l+1+count,i_r); return root; }};
2)递归条件为i_l==i_r时:
class Solution {public: TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) { if(preorder.empty()) return NULL; TreeNode* root=buildTree_(preorder,0,preorder.size(),inorder,0,inorder.size()); return root; } TreeNode* buildTree_(vector<int>& preorder,int p_l,int p_r,vector<int>& inorder,int i_l,int i_r){ if(i_l==i_r) return NULL; TreeNode* root=new TreeNode(preorder[p_l]); int count=0; for(int i=i_l;i<i_r;i++){ if(inorder[i]==preorder[p_l]) break; count++; } root->left=buildTree_(preorder,p_l+1,p_l+count+1,inorder,i_l,i_l+count); root->right=buildTree_(preorder,p_l+count+1,p_r,inorder,i_l+1+count,i_r); return root; }};
阅读全文
0 0
- C++之根据组合遍历顺序生成二叉树(25)---《那些奇怪的算法》
- C++之平衡二叉树的判定(25)---《那些奇怪的算法》
- C++之二叉排序树(二叉搜索树)的实现(4)---《那些奇怪的算法》
- C++之统计二叉树中路径和为定值的所有路径(24)---《那些奇怪的算法》
- 二叉树-根据已知的遍历顺序还原二叉树
- C++之树的层序遍历和深度优先遍历(先序遍历)(14)---《那些奇怪的算法》
- C++实现二叉树的镜像操作(18)---《那些奇怪的算法》
- C++之排序算法(2)---《那些奇怪的算法》
- 根据已知二叉树的遍历顺序,求另一种遍历顺序
- C++之求解二叉树中所有从根节点到叶节点的所有路径(23)---《那些奇怪的算法》
- C++之实现大顶堆(1)---《那些奇怪的算法》
- C++之快速排序(3)---《那些奇怪的算法》
- C++之堆排序(6)---《那些奇怪的算法》
- C++之归并排序(7)---《那些奇怪的算法》
- c语言实现顺序二叉树的非递归的三种遍历算法
- offer面试题---根据前序遍历和中序遍历的顺序输出二叉树
- 根据数组,生成数组中元素的所有组合,(顺序,与非顺序)
- 数据结构 - 二叉树的广度优先遍历算法(C++)
- 比特酒吧 BTC98币行(BTC 、LTC、LTH、ETC) 简介
- [BZOJ2850]巧克力王国(KD-tree)
- 删除Hyper-V 多个vEthernet适配器
- 机器学习基础:分类vs回归
- Rdis快速入门
- C++之根据组合遍历顺序生成二叉树(25)---《那些奇怪的算法》
- Spring Cloud教程合集
- vue-router
- 自定义view实现画折线图
- 基于STM32开发板电机控制的学习
- 配置多台机器SSH相互通信信任,以及一个自动备份脚本例子
- 【数据仓库】数据仓库设计前如何粗估所需的存储空间大小?
- Struts与Apache Tiles结合实现像HTML一样的Frame框架
- 内存管理最先分配算法