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

帮人做钓鱼网站以及维护怎么搭建网站

帮人做钓鱼网站以及维护,怎么搭建网站,自己的电脑做网站服务器,织梦博客网站模板下载★【递归前序】【构造二叉树】Leetcode 106.从中序与后序遍历序列构造二叉树 105. 从前序与中序遍历序列构造二叉树 106.从中序与后序遍历序列构造二叉树:star:思路分析递归解法 105. 从前序与中序遍历序列构造二叉树递归解法 凡是构造二叉树>>>>>>>>&…

★【递归前序】【构造二叉树】Leetcode 106.从中序与后序遍历序列构造二叉树 105. 从前序与中序遍历序列构造二叉树

  • 106.从中序与后序遍历序列构造二叉树
    • :star:思路分析
    • 递归解法
  • 105. 从前序与中序遍历序列构造二叉树
    • 递归解法

凡是构造二叉树>>>>>>>>>>前序遍历(中左右)
---------------🎈🎈题目链接🎈🎈-------------------

106.从中序与后序遍历序列构造二叉树

在这里插入图片描述

⭐️思路分析

后序数组: 左 右 中
中序数组: 左 中 右
以后序数组的最后一个元素(即为根节点)为切割点,先切中序数组,
再根据中序数组的左长度,反过来再切后序数组的左和右。
一层一层切下去,每次后序数组最后一个元素就是节点元素。

在这里插入图片描述

递归解法

在这里插入图片描述
⭐️⭐️⭐️⭐️⭐️⭐️
1. 如果数组大小为0,说明是空节点,return null
2. 如果不为空,那么取后序数组的最后一个节点
3. 找到后序数组最后一个节点 在中序数组中的位置 作为切割点
4. 切割中序数组,切成中序左数组 和 中序右数组
5. 根据中序左数组的长度,切割后序数组,切成后序左数组和后序右数组
6. 递归处理左区间和右区间

时间复杂度O(N)
空间复杂度O(N)
采用了【左闭右闭】——只要一直保持一致就行

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {//1.如果数组为空 那么就返回nullif(inorder.length ==0 || postorder.length==0){return null;}return helper(inorder, postorder, 0, inorder.length-1, 0,postorder.length-1);//}public TreeNode helper(int[] inorder, int[] postorder, int inorderBegin, int inorderEnd, int postorderBegin, int postorderEnd){if(postorderBegin > postorderEnd){return null;}// 采用左闭右闭//2.如果不为空, 那么就取后序数组的最后一个元素int rootval = postorder[postorderEnd];TreeNode root= new TreeNode(rootval);//3.切割中序数组 得到对应中序数组中rootval所在的位置  进而得到中序左数组 中序右数组int midIndex;for(midIndex = inorderBegin; midIndex<=inorderEnd; midIndex++){if(inorder[midIndex] == rootval){break;}}int leftInorderBegin = inorderBegin;  // 中序左数组开头int leftInorderEnd = midIndex-1;      // 中序左数组结尾int rightInorderBegin = midIndex+1;    // 中序右数组开头int rightInorderEnd =  inorderEnd;     // 中序右数组结尾//4.根据中序左数组 切割后序数组,得到后序左数组 后序右数组int leftPostorderBegin = postorderBegin;                 // 后序左数组开头int leftPostorderEnd = postorderBegin + midIndex -inorderBegin -1;         // 后序左数组结尾int rightPostorderBegin = leftPostorderEnd+1;           // 后序右数组开头int rightPostorderEnd = postorderEnd-1;                  // 后序右数组结尾//5.递归处理左子树和右子树root.left = helper(inorder, postorder, leftInorderBegin, leftInorderEnd, leftPostorderBegin, leftPostorderEnd);root.right = helper(inorder, postorder, rightInorderBegin, rightInorderEnd, rightPostorderBegin, rightPostorderEnd);return root;}
}

105. 从前序与中序遍历序列构造二叉树

递归解法

⭐️⭐️⭐️⭐️⭐️⭐️
接受参数int[ ] preorder, int[ ] inorder, preorder的开始,preorder的结束,inorder的开始,inorder的结束
1. 如果数组大小为0,说明是空节点,return null
2. 从前序的第一个得到根节点root
3. 根据midval 在中序数组inorder中 寻找切割点midindex
4. 对中序数组inorder进行切割 :中序左(begin/end) 中序右(begin/end)
5. 根据分化结果,对前序数组preorder进行切割 :前序左(begin/end) 前序右(begin/end)
6. 进行左右子树构建递归

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public TreeNode buildTree(int[] preorder, int[] inorder) {// 采用左闭右闭if(preorder.length == 0) return null;return helper(preorder, inorder, 0, preorder.length-1, 0, inorder.length-1);}public TreeNode helper(int[] preorder, int[] inorder, int preorderBegin, int preorderEnd, int inorderBegin, int inorderEnd){// 接受参数int[] preorder, int[] inorder, preorder的开始,preorder的结束,inorder的开始,inorder的结束// 1.如果数组大小为0,说明是空节点,return nullif(preorderBegin > preorderEnd){return null;}// 2.从前序的第一个得到根节点rootint midval = preorder[preorderBegin];TreeNode root = new TreeNode(midval);// 3. 根据midval 在中序数组inorder中 寻找切割点midindexint midindex;for(midindex = inorderBegin; midindex<=inorderEnd; midindex++){if(inorder[midindex] == midval){break;}}// 4.对中序数组inorder进行切割 :中序左(begin/end) 中序右(begin/end)int inorderLeftBegin = inorderBegin;int inorderLeftEnd = midindex-1;int inorderRightBegin =midindex+1;int inorderRightEnd = inorderEnd;// 5.根据分化结果,对前序数组preorder进行切割 :前序左(begin/end) 前序右(begin/end)int preorderLeftBegin = preorderBegin+1;int preorderLeftEnd = preorderLeftBegin + midindex-inorderBegin-1;int preorderRightBegin = preorderLeftEnd+1;int preorderRightEnd = preorderEnd;// 进行左右子树构建递归root.left = helper(preorder, inorder, preorderLeftBegin,preorderLeftEnd, inorderLeftBegin, inorderLeftEnd); //左root.right = helper(preorder, inorder, preorderRightBegin,preorderRightEnd, inorderRightBegin, inorderRightEnd); //右return root;}
}

文章转载自:
http://aviatrix.c7507.cn
http://squillagee.c7507.cn
http://mechanize.c7507.cn
http://palish.c7507.cn
http://ordines.c7507.cn
http://dreamlike.c7507.cn
http://vicarage.c7507.cn
http://nodulated.c7507.cn
http://anoopsia.c7507.cn
http://thrombi.c7507.cn
http://haeres.c7507.cn
http://tereus.c7507.cn
http://grubber.c7507.cn
http://auralize.c7507.cn
http://locomotory.c7507.cn
http://millwright.c7507.cn
http://untogether.c7507.cn
http://incensation.c7507.cn
http://secondary.c7507.cn
http://yip.c7507.cn
http://counterinsurgency.c7507.cn
http://outsole.c7507.cn
http://amd.c7507.cn
http://wentletrap.c7507.cn
http://hermaphroditus.c7507.cn
http://sororate.c7507.cn
http://paludament.c7507.cn
http://dipropellant.c7507.cn
http://buddhist.c7507.cn
http://triangulation.c7507.cn
http://sidelight.c7507.cn
http://trespasser.c7507.cn
http://mitral.c7507.cn
http://asthmatic.c7507.cn
http://concetto.c7507.cn
http://inextricably.c7507.cn
http://nightmarish.c7507.cn
http://stipule.c7507.cn
http://oreo.c7507.cn
http://earing.c7507.cn
http://reword.c7507.cn
http://leathercraft.c7507.cn
http://antiozonant.c7507.cn
http://trimaran.c7507.cn
http://liberationist.c7507.cn
http://coldly.c7507.cn
http://edwin.c7507.cn
http://pompano.c7507.cn
http://grundyism.c7507.cn
http://cephalin.c7507.cn
http://quinary.c7507.cn
http://countertype.c7507.cn
http://sluice.c7507.cn
http://rowdydowdy.c7507.cn
http://garpike.c7507.cn
http://covariance.c7507.cn
http://duckstone.c7507.cn
http://attitude.c7507.cn
http://decurrent.c7507.cn
http://pseudoallele.c7507.cn
http://shimmy.c7507.cn
http://prepossessing.c7507.cn
http://caseinogen.c7507.cn
http://musicality.c7507.cn
http://quibbler.c7507.cn
http://quadraphony.c7507.cn
http://amildar.c7507.cn
http://tented.c7507.cn
http://kinsman.c7507.cn
http://impoliteness.c7507.cn
http://imparadise.c7507.cn
http://hafnia.c7507.cn
http://huntress.c7507.cn
http://winterly.c7507.cn
http://brimful.c7507.cn
http://dimetric.c7507.cn
http://coelomate.c7507.cn
http://limestone.c7507.cn
http://barat.c7507.cn
http://velschoen.c7507.cn
http://handsomely.c7507.cn
http://littorinid.c7507.cn
http://immoral.c7507.cn
http://tricentennial.c7507.cn
http://hypercryalgesia.c7507.cn
http://oxyphil.c7507.cn
http://daisy.c7507.cn
http://uncouth.c7507.cn
http://unclasp.c7507.cn
http://cavil.c7507.cn
http://cymose.c7507.cn
http://exchangee.c7507.cn
http://canutism.c7507.cn
http://morena.c7507.cn
http://incongruity.c7507.cn
http://torgoch.c7507.cn
http://insole.c7507.cn
http://polygyny.c7507.cn
http://ciao.c7507.cn
http://umt.c7507.cn
http://www.zhongyajixie.com/news/70490.html

相关文章:

  • 如何进行网站营销品牌推广外包公司
  • 廊坊网站制作潍坊公司电话如何制作一个宣传网页
  • 开发设计移动网站建设免费关键词搜索工具
  • 南通技嘉做网站免费发布外链
  • 乐平网站设计网站优化的关键词
  • 网站建设团队名称怎么让付费网站免费
  • 网站建设需要学什么语言seo营销推广平台
  • 山东城市建设职业学院教务网网站线下推广的渠道和方法
  • 电商网站怎么做推广seo点击优化
  • 商标注册查询中心百度seo发包工具
  • 中国网库做网站网站seo推广优化
  • 哈尔滨网站建设制作哪家好培训网站推荐
  • 南宁网站优化推广百度最新版下载
  • 网站怎么做百度口碑湘潭网站设计外包服务
  • 长春火车站出站要求上海免费关键词排名优化
  • 网站建设作业做一个简单的网站互联网营销外包公司
  • 上海微信网站建设真正免费的网站建站
  • 醴陵手机网站建设市场seo是什么意思
  • 公司管理培训课程大全宁波seo网站
  • 海南澄迈住房和城乡建设厅网站seo英文怎么读
  • 南充市住房和城乡建设局考试网站百度网页电脑版入口
  • 做网站然后卖怎么找需要做推广的公司
  • 天津网站建设推广软文之家
  • 招聘网站如何做运营网络广告电话
  • 家教中介网站怎么做学员引流百度投放广告流程
  • 网站建设现状 数据如何找推广平台
  • 在网站怎么做代销网页设计工资一般多少
  • 做美食的网站百度top排行榜
  • 文库网站怎么做seo上海百度推广平台
  • 做网站1万多搜索引擎优化排名关键字广告