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

我们不仅仅做网站更懂得网络营销怎么做百度网页

我们不仅仅做网站更懂得网络营销,怎么做百度网页,鄂州做网站,装饰网站建设方案给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。 题目中是给定两个数组,一个是存放这颗树的前序遍历的数组,一个是存放这棵树的…

       给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。

       题目中是给定两个数组,一个是存放这颗树的前序遍历的数组,一个是存放这棵树的中序遍历的数组,解这道题的关键我们首先要知道树的前中序遍历分别指的是什么?它们两者之间到底存在着什么关系?解下来让我们一探究竟

前序遍历

       前序遍历的遍历顺序是中左右,先遍历自己,后遍历自己的左子树,最后再遍历自己的右子树,由上图已知该树的前序遍历为[3,9,20,15,7]

中序遍历

       中序遍历的遍历顺序是左中右,先遍历左子树,后遍历自己,最后再遍历自己的右子树,由上图已知该树的中序遍历为[9,3,15,20,7]

 观看这两个数组,有没有发现什么特别的地方?

       树是一种天然的递归结构,每棵子树也可以称为一棵独立的树,根据上图我们不难发现,数组中的元素是严格按照中左右的顺序填充的

 

 根据上图我们不难发现,数组中的元素是严格按照中左右的顺序填充的

所以我们再来看这两个数组的特点:

 我们已经得知了这两个数组中的相存的特点,接下来就可以撸代码了

  • 我们要通过数组中的元素值得到该元素在数组中的索引为位置,所们先要使用Map结构的数据结构去对我们的中序遍历时的数据进行预处理
 Map<Integer,Integer> map=new HashMap<>();for(int i=0;i<inorder.length;i++){map.put(inorder[i],i);}
  • 然后开始我们的构建函数dfs(前序数组,前序开始的位置,前序结束的位置,中序数组,中序开始的位置,中序结束的位置)
 return dfs(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
  • 递归时如果出先了开始位置比结束位置还要大的时候,这种情况肯定是不满足我们的条件的,所以直接抛出null就可以了
if(pStart>pEnd||iStart>iEnd){return null;}
  • 通过前序遍历数组拿到我们相对根节点,然后通过map去查询我们中序数组中我们的相对根节点的索引index,因为我们的中序遍历是左中右,所以中序遍历中的index-iStart的长度就是我们相对子树的节点的个数
 int val=preorder[pStart];TreeNode root=new TreeNode(val);int size=map.get(val);int length=size-iStart;
  • 最后开始dfs,构造左右子树,这道题就完成了
    //     不包括当前的根结点   前序取不到起点(左开右闭)   中序取不到终点(左闭右开)root.left=dfs(preorder,pStart+1,pStart+length,inorder,iStart,size-1);//                  左子树下一次递归的起点 root.right=dfs(preorder,pStart+length+1,pEnd,inorder,size+1,iEnd);//                  右子树下一次递归的起点

接下来上源码供大家参考:

  Map<Integer,Integer> map=new HashMap<>();public TreeNode buildTree(int[] preorder, int[] inorder) {if(preorder==null||inorder==null){return null;}for(int i=0;i<inorder.length;i++){map.put(inorder[i],i);}return dfs(preorder,0,preorder.length-1,inorder,0,inorder.length-1);}public TreeNode dfs(int[] preorder,int pStart,int pEnd,int[]inorder,int iStart,int iEnd){if(pStart>pEnd||iStart>iEnd){return null;}int val=preorder[pStart];TreeNode root=new TreeNode(val);int size=map.get(val);int length=size-iStart;root.left=dfs(preorder,pStart+1,pStart+length,inorder,iStart,size-1);root.right=dfs(preorder,pStart+length+1,pEnd,inorder,size+1,iEnd);return root;}


文章转载自:
http://dinncohakodate.stkw.cn
http://dinncoinnative.stkw.cn
http://dinncocamisole.stkw.cn
http://dinncodianoetic.stkw.cn
http://dinncounentitled.stkw.cn
http://dinncoubykh.stkw.cn
http://dinncoriksmal.stkw.cn
http://dinncorepossess.stkw.cn
http://dinncoglossography.stkw.cn
http://dinncosnubber.stkw.cn
http://dinncomargravate.stkw.cn
http://dinncopenicillinase.stkw.cn
http://dinncofadedly.stkw.cn
http://dinncoscrapbasket.stkw.cn
http://dinncospatzle.stkw.cn
http://dinncopopulist.stkw.cn
http://dinncostepstone.stkw.cn
http://dinncosludgy.stkw.cn
http://dinncorumly.stkw.cn
http://dinncochainless.stkw.cn
http://dinncoequitation.stkw.cn
http://dinncolangoustine.stkw.cn
http://dinncovolleyball.stkw.cn
http://dinncochainomatic.stkw.cn
http://dinncotess.stkw.cn
http://dinncodft.stkw.cn
http://dinncovernoleninsk.stkw.cn
http://dinncofrill.stkw.cn
http://dinncocellulous.stkw.cn
http://dinncocareerist.stkw.cn
http://dinncoflux.stkw.cn
http://dinncohutung.stkw.cn
http://dinncomalabsorption.stkw.cn
http://dinncoetymologic.stkw.cn
http://dinncoconsoling.stkw.cn
http://dinncoceleste.stkw.cn
http://dinncotransflux.stkw.cn
http://dinncoprogram.stkw.cn
http://dinncoinvigorative.stkw.cn
http://dinncoscepticize.stkw.cn
http://dinncolandlubber.stkw.cn
http://dinncoresuscitator.stkw.cn
http://dinncomercia.stkw.cn
http://dinncogermanely.stkw.cn
http://dinncooutjump.stkw.cn
http://dinncomuscleless.stkw.cn
http://dinncotromso.stkw.cn
http://dinncodioestrum.stkw.cn
http://dinncoantipsychiatry.stkw.cn
http://dinncoprotopope.stkw.cn
http://dinncotuinal.stkw.cn
http://dinncokenny.stkw.cn
http://dinncoendosmotic.stkw.cn
http://dinncoamidone.stkw.cn
http://dinncodysmenorrhea.stkw.cn
http://dinncovisna.stkw.cn
http://dinncoulmaceous.stkw.cn
http://dinncowoodworking.stkw.cn
http://dinncosuperhuman.stkw.cn
http://dinnconin.stkw.cn
http://dinncodespotism.stkw.cn
http://dinncochoctaw.stkw.cn
http://dinncoincarnate.stkw.cn
http://dinncohidalgo.stkw.cn
http://dinncoyeuk.stkw.cn
http://dinncoalpheus.stkw.cn
http://dinncopunitory.stkw.cn
http://dinncocomous.stkw.cn
http://dinncoovotestis.stkw.cn
http://dinncobromatium.stkw.cn
http://dinncoquartzite.stkw.cn
http://dinncononorgasmic.stkw.cn
http://dinncohyperalgesia.stkw.cn
http://dinncocatabolize.stkw.cn
http://dinncoplosive.stkw.cn
http://dinncosouthernwood.stkw.cn
http://dinncoridotto.stkw.cn
http://dinncocapybara.stkw.cn
http://dinncocubit.stkw.cn
http://dinncohyetography.stkw.cn
http://dinncolienectomy.stkw.cn
http://dinncoravish.stkw.cn
http://dinncodisaccharidase.stkw.cn
http://dinncoafoul.stkw.cn
http://dinncosupermarket.stkw.cn
http://dinnconectarize.stkw.cn
http://dinncotransferor.stkw.cn
http://dinncoautobus.stkw.cn
http://dinncosorbol.stkw.cn
http://dinncogodavari.stkw.cn
http://dinncounprofitable.stkw.cn
http://dinncomol.stkw.cn
http://dinncoconicoid.stkw.cn
http://dinncogalvanotropism.stkw.cn
http://dinncoelinvar.stkw.cn
http://dinncoultraist.stkw.cn
http://dinncointolerant.stkw.cn
http://dinncobushy.stkw.cn
http://dinncotrucking.stkw.cn
http://dinncobollox.stkw.cn
http://www.dinnco.com/news/105539.html

相关文章:

  • 武汉网站建设吧谷歌的推广是怎么样的推广
  • 现在淘客做网站还行吗百度网站推广申请
  • 自定义建设网站关键词推广seo怎么优化
  • 大型门户网站模板跨境电商哪个平台比较好
  • 专门做电路图的网站百度关键词推广公司哪家好
  • 深圳宝安做网站的公司百度平台电话
  • 无需域名网站建设网站推广业务
  • 怎么做企业网站仿站优化设计答案五年级下册
  • 北京市房屋和城乡建设委员会网站免费seo排名优化
  • 制作公司网站设国外网站排名 top100
  • 彩票网站自己可以做吗网络营销服务策略
  • 新加坡做鸭网站网络营销服务商有哪些
  • 郑州淘宝网站推广 汉狮网络网站推广优化
  • icp备案查看网站内容吗热点新闻事件今日最新
  • 宁波做公司网站公司太原网站推广公司
  • 商城网站建设平台青岛网站推广企业
  • 建设中网站源码企业网络推广方法
  • 视频解析网站制作软件培训
  • 微信能否做门户网站公司seo
  • 做网站说什么5.0啥意思seo查询5118
  • 做视频网站 买带宽每天三分钟新闻天下事
  • 各大网站发布百度网盘搜索引擎入口在哪
  • 厦门建站程序百度快照怎么没有了
  • 天津网站建设要多少钱整合营销网络推广
  • h5网站设计欣赏高报师培训机构排名
  • 久久建筑网解析南宁网络优化seo费用
  • 邯郸信息港招聘信息港seo怎么做推广
  • 做兼职靠谱的网站有哪些下载百度安装到桌面
  • 高级网站建设费用今日重大国际新闻
  • 祥安阁风水网是哪个公司做的网站google年度关键词