当前位置: 首页 > news >正文

兵团建设环保局网站/网站建设关键词排名

兵团建设环保局网站,网站建设关键词排名,搜索推广策略制定,网站后台管理系统怎么做在数据结构中,我们经常需要根据不同的组合遍历顺序确定一个二叉树,并生成它,现在就来整理一下思路,这儿以中序遍历和后序遍历为例! 如上图所示,我们先由于后序遍历找到根结点,然后在中序遍历的序…

在数据结构中,我们经常需要根据不同的组合遍历顺序确定一个二叉树,并生成它,现在就来整理一下思路,这儿以中序遍历和后序遍历为例!

这里写图片描述
如上图所示,我们先由于后序遍历找到根结点,然后在中序遍历的序列中找到对应的根结点,此时可以将中序遍历和后续遍历中左右子树的位置确定,然后递归进行划分,注意递归的结束条件的设置!
实现代码如下:

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;}
};
http://www.lbrq.cn/news/950707.html

相关文章:

  • 广州海珠做网站/网站推广的基本方法是
  • 宁波高端网站建设联系方式/百度关键词价格查询
  • 卡当网站建设/西安seo引擎搜索优化
  • 做搜索引擎网站/北京度seo排名
  • 南阳做网站公司哪家好/图片外链上传网站
  • wordpress文章页面添加广告/seo合作代理
  • 网站制作报价/九江seo公司
  • 如何在人力资源网站做合同续签/hao123上网从这里开始官方
  • 做昆特牌的网站/网站搜索优化官网
  • 优秀网页案例/东莞百度seo关键词优化
  • 网站制作上哪学校/百度推广官网网站
  • 可以发外链的网站或平台有哪些/seo是哪个英文的简写
  • 成都企业网站建设哪家专业/网站及搜索引擎优化建议
  • 招标网站哪个好/高质量发展服务业
  • 用struts2框架做的网站/长沙优化排名推广
  • 做外贸的物流网站/可以免费打开网站的软件
  • 农村建设集团有限公司网站/站长工具app下载
  • 淘客做的网站属于什么类型/北京网络营销推广外包
  • 学室内装潢设计哪个学校好/济南网站万词优化
  • 企业网站优化应该怎么做/杭州百度seo优化
  • 网站title keywords/长尾词挖掘工具
  • 专业做毕业设计网站设计/青岛模板建站
  • 高新区免费网站建设/时事政治2023最新热点事件
  • 高性价比网站建设/2345导航网址
  • 国内免费建站网站/杭州seo教程
  • 做网站骗/星巴克营销策划方案
  • 360网站安全检测/东莞网站建设推广
  • ps做电商网站流程图/百度网页排名怎么提升
  • 网站开发类型/百度上搜索关键词如何在首页
  • 企业网站开发制作/合肥360seo排名
  • React探索高性能Tree树组件实现——react-window、react-vtree
  • 将 RustFS 用作 GitLab 对象存储后端
  • 零基础学习性能测试第三章:执行性能测试
  • Softhub软件下载站实战开发(十九):软件信息展示
  • 技术演进中的开发沉思-40 MFC系列:多线程协作
  • Chris Fraser | 中国早期思想中墨家与荀子的知识论