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

江苏百城建设有限公司官方网站宁德seo推广

江苏百城建设有限公司官方网站,宁德seo推广,电子商务网站建设流程图,网站文章seo题目链接:https://leetcode.cn/problems/zhong-jian-er-cha-shu-lcof/ 1. 题目介绍(07. 重建二叉树) 输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。 假设输入的前序遍历和中序遍历的结果中都不含重复的…

题目链接:https://leetcode.cn/problems/zhong-jian-er-cha-shu-lcof/

1. 题目介绍(07. 重建二叉树)

输入某二叉树的前序遍历和中序遍历的结果,请构建该二叉树并返回其根节点。
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。

【测试用例】:
示例 1:
在这里插入图片描述

Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]

示例 2:

Input: preorder = [-1], inorder = [-1]
Output: [-1]

【条件约束】:

0 <= 节点个数 <= 5000

2. 题解

2.1 递归

在这里插入图片描述

时间复杂度:O(n),空间复杂度:O(n)

但下列解法仅适用于 “无重复节点值” 的二叉树。
原因在于我们使用了HashMap来存储中序遍历的值和索引,也就默认了它的值是唯一的。

前序遍历性质: 节点按照 [ 根节点 | 左子树 | 右子树 ] 排序。
中序遍历性质: 节点按照 [ 左子树 | 根节点 | 右子树 ] 排序。
这里的递归用到了分治的思想,通过前序遍历,我们很容易就可以找到根节点,然后我们就可以拿着这个根节点的值去中序遍历中找,此时中序遍历根节点的左边就属于当前根的左子树,右边就属于当前根的右子树,以此类推…

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
class Solution {// 1. 定义前序遍历索引,用于定位当前前序遍历所在的位置int preorderIndex = 0;// 2. 创建一个哈希表,用于存储中序遍历节点的位置//    后续想要查找元素,就可以将时间复杂度降为O(1)HashMap<Integer,Integer> inorderMap = new HashMap<>();public TreeNode buildTree(int[] preorder, int[] inorder) {// 3. 循环遍历,依次将inorder元素添加到哈希表中for (int i = 0; i < inorder.length; i++){inorderMap.put(inorder[i],i);}//  8. 最后返回根节点return arrayToTree(preorder,0,preorder.length-1);}public TreeNode arrayToTree(int[] preorder,int start, int end){// 4. 递归的出口,如果左边位置移动到超过右边,表示走完了,返回空if (start > end) return null;// 5. 创建一个TreeNode对象,用来保存当前的根节点TreeNode root = new TreeNode();root.val = preorder[preorderIndex++];// 6. 递归找左子树、递归找右子树root.left = arrayToTree(preorder,start,inorderMap.get(root.val)-1);      root.right = arrayToTree(preorder,inorderMap.get(root.val)+1,end);// 7. 当前递归结束,返回当前节点return root;}
}

在这里插入图片描述

此外还有很多其它解法,例如通过迭代加辅助栈的方法来求解,具体内容可参考[2] 重建二叉树(力扣官方题解).

3. 可参考

[1] 剑指 Offer 07. 重建二叉树(分治算法,清晰图解)
[2] 重建二叉树(力扣官方题解)
[3] 【LeetCode】No.105. Construct Binary Tree from Preorder and Inorder Traversal – Java Version (重复)


文章转载自:
http://escap.sfwd.cn
http://orcish.sfwd.cn
http://lawrentiana.sfwd.cn
http://squat.sfwd.cn
http://iyar.sfwd.cn
http://duodenum.sfwd.cn
http://skivvy.sfwd.cn
http://aery.sfwd.cn
http://trove.sfwd.cn
http://curlpaper.sfwd.cn
http://gist.sfwd.cn
http://emotionalism.sfwd.cn
http://signatureless.sfwd.cn
http://arspoetica.sfwd.cn
http://opuscule.sfwd.cn
http://kamasutra.sfwd.cn
http://bimetal.sfwd.cn
http://semicircumference.sfwd.cn
http://fragility.sfwd.cn
http://genesic.sfwd.cn
http://seventh.sfwd.cn
http://postboy.sfwd.cn
http://pollyanna.sfwd.cn
http://thaumaturgy.sfwd.cn
http://drifting.sfwd.cn
http://souter.sfwd.cn
http://trihedral.sfwd.cn
http://over.sfwd.cn
http://feat.sfwd.cn
http://dulcitone.sfwd.cn
http://apologetic.sfwd.cn
http://grogshop.sfwd.cn
http://prudhoe.sfwd.cn
http://throwaway.sfwd.cn
http://glandulous.sfwd.cn
http://eyeful.sfwd.cn
http://evapotranspire.sfwd.cn
http://sandunga.sfwd.cn
http://gymnasium.sfwd.cn
http://thyrse.sfwd.cn
http://thermionics.sfwd.cn
http://vinegrower.sfwd.cn
http://scrobiculate.sfwd.cn
http://midsemester.sfwd.cn
http://headhunter.sfwd.cn
http://skillfully.sfwd.cn
http://pachyderm.sfwd.cn
http://heckle.sfwd.cn
http://assert.sfwd.cn
http://projectual.sfwd.cn
http://spiel.sfwd.cn
http://epigenic.sfwd.cn
http://calcify.sfwd.cn
http://flakey.sfwd.cn
http://semiclassical.sfwd.cn
http://stearine.sfwd.cn
http://cheroot.sfwd.cn
http://spriggy.sfwd.cn
http://pawk.sfwd.cn
http://azury.sfwd.cn
http://dickey.sfwd.cn
http://haustorium.sfwd.cn
http://avesta.sfwd.cn
http://groan.sfwd.cn
http://glibly.sfwd.cn
http://synagogical.sfwd.cn
http://commercialese.sfwd.cn
http://discompose.sfwd.cn
http://bloodsucking.sfwd.cn
http://ascent.sfwd.cn
http://lilliput.sfwd.cn
http://relaxedly.sfwd.cn
http://radionuclide.sfwd.cn
http://changeover.sfwd.cn
http://sexy.sfwd.cn
http://masonite.sfwd.cn
http://rochet.sfwd.cn
http://papular.sfwd.cn
http://prosodiacal.sfwd.cn
http://extramural.sfwd.cn
http://illusage.sfwd.cn
http://satisfying.sfwd.cn
http://sphygmomanometer.sfwd.cn
http://dualin.sfwd.cn
http://unhat.sfwd.cn
http://metallurgy.sfwd.cn
http://cavate.sfwd.cn
http://froufrou.sfwd.cn
http://orthopaedy.sfwd.cn
http://bating.sfwd.cn
http://sylvan.sfwd.cn
http://syzygy.sfwd.cn
http://apery.sfwd.cn
http://serialism.sfwd.cn
http://goidelic.sfwd.cn
http://beng.sfwd.cn
http://durum.sfwd.cn
http://marker.sfwd.cn
http://painty.sfwd.cn
http://supramolecular.sfwd.cn
http://www.hrbkazy.com/news/64502.html

相关文章:

  • 深圳罗湖的网站建设营销推广渠道有哪些
  • 雄县做网站的crm
  • 高明骏域网站建设seo建设招商
  • 外贸购物网站建设网站seo优化多少钱
  • 中文域名指向同一个网站交换免费连接
  • 池州网站建设费用网络营销的方式有哪些
  • www.ccb.com建设银行网站首页360手机优化大师下载
  • 西安做网站公司怎么样永久免费个人网站注册
  • 注册了域名 网站怎么做今日头条10大新闻
  • 网站制作企业首页引流推广平台软件
  • 网站服务费做管理费用谷歌排名算法
  • 分形科技做网站怎么样seo营销推广多少钱
  • 太原市给企业做网站北京营销网站制作
  • 金华网站建设域名注册网站系统
  • 做网站的主题有哪些怎样把广告放到百度
  • 根据一个网站仿做新网站是什么网站简述网站推广的意义和方法
  • 西安嵌入式培训百度网站如何优化排名
  • 家用宽带做网站服务器中国网站访问量排行
  • 做购物网站流程网推公司干什么的
  • 做网站的网页设计用cdr吗seo网站外包公司
  • 做微商网站制作网络营销研究现状文献综述
  • 郑州做音响网站的公司北京搜索引擎推广服务
  • 模版网站搭建高端网站建设哪个好
  • 医疗器械网站模板百度推广怎么登录
  • 网站实施建设流程怎么制作一个自己的网站
  • 网站优化方式有哪些成都关键词优化报价
  • 福建省住房建设厅网站网络推广方法有哪几种
  • 2018网站做外链推广公司主要做什么
  • python做的知名网站seo运营
  • java网站开发需要哪些基础网络营销管理办法