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

小白力扣算法题day07-二叉树

想不出来,看答案做的,利用递归和哈希表想不到。

class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { // 第一步:创建一个哈希表(字典),用来快速找到中序遍历中某个值的位置 // 为什么要这样做?因为在中序遍历中找根节点的位置时,如果每次都遍历数组,会很慢 // 用哈希表可以在 O(1) 时间内找到位置 Map<Integer, Integer> inorderMap = new HashMap<>(); // 遍历中序遍历数组,把每个值和它的索引存到哈希表中 // 例如 inorder = [9,3,15,20,7] // 那么 map 里就会存:{9:0, 3:1, 15:2, 20:3, 7:4} for (int i = 0; i < inorder.length; i++) { inorderMap.put(inorder[i], i); } // 调用递归函数,开始构建二叉树 // 参数说明: // preorder: 前序遍历数组 // 0: 前序遍历的起始索引 // preorder.length - 1: 前序遍历的结束索引 // inorder: 中序遍历数组 // 0: 中序遍历的起始索引 // inorder.length - 1: 中序遍历的结束索引 // inorderMap: 哈希表,用来快速定位根节点在中序遍历中的位置 return build(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, inorderMap); } private TreeNode build(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, Map<Integer, Integer> inorderMap) { // 递归终止条件:如果起始索引大于结束索引,说明这个区间没有节点了 // 例如左子树没有节点时,inStart > inEnd,返回 null if (preStart > preEnd || inStart > inEnd) { return null; } // 前序遍历的第一个节点就是当前子树的根节点 // 例如 preorder = [3,9,20,15,7],第一个元素 3 就是根节点 int rootVal = preorder[preStart]; TreeNode root = new TreeNode(rootVal); // 创建根节点 // 在中序遍历中找到根节点的位置 // 例如 inorder = [9,3,15,20,7],根节点 3 在索引 1 的位置 int rootIndexInorder = inorderMap.get(rootVal); // 计算左子树的节点个数 // 左子树的节点数 = 根节点在中序遍历中的位置 - 中序遍历的起始位置 // 例如:inStart=0, rootIndexInorder=1,那么左子树有 1-0=1 个节点(就是 9) int leftSize = rootIndexInorder - inStart; // ========== 构建左子树 ========== // 左子树的前序遍历范围: // 起始索引:preStart + 1(跳过当前的根节点) // 结束索引:preStart + leftSize(从前序中取 leftSize 个节点给左子树) // 左子树的中序遍历范围: // 起始索引:inStart // 结束索引:rootIndexInorder - 1(根节点左边都是左子树的节点) root.left = build(preorder, preStart + 1, preStart + leftSize, inorder, inStart, rootIndexInorder - 1, inorderMap); // ========== 构建右子树 ========== // 右子树的前序遍历范围: // 起始索引:preStart + leftSize + 1(跳过左子树的所有节点) // 结束索引:preEnd // 右子树的中序遍历范围: // 起始索引:rootIndexInorder + 1(根节点右边都是右子树的节点) // 结束索引:inEnd root.right = build(preorder, preStart + leftSize + 1, preEnd, inorder, rootIndexInorder + 1, inEnd, inorderMap); // 返回构建好的根节点 return root; } }

class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { Map<Integer,Integer>map=new HashMap<>(); for (int i=0;i<inorder.length;i++){ map.put(inorder[i],i); } return build(inorder,0,inorder.length-1, postorder,0,postorder.length-1,map); } private TreeNode build(int[] inorder,int inStart,int inEnd, int[] postorder,int poStart,int poEnd,Map<Integer,Integer>map){ if(inStart>inEnd||poStart>poEnd){ return null; } int rootVal=postorder[poEnd]; TreeNode root=new TreeNode(rootVal); int rootInorderIndex=map.get(rootVal); int leftSize=rootInorderIndex-inStart; root.left=build(inorder,inStart,rootInorderIndex-1, postorder,poStart,poStart+leftSize-1,map); root.right=build(inorder,rootInorderIndex+1,inEnd, postorder,poStart+leftSize,poEnd-1,map); return root; } }

相同原理加以巩固。


class Solution { public boolean hasPathSum(TreeNode root, int targetSum) { if(root==null)return false; if(root.left==null&&root.right==null){ return targetSum== root.val; } int remain=targetSum-root.val; return hasPathSum(root.left,remain)||hasPathSum(root.right,remain); } }

递归的应用

http://www.cnnetsun.cn/news/2064660.html

相关文章:

  • 从运维角度,如何通过IP查询工具进行故障排查?实操边界与落地步骤
  • 原生广告:APP变现的“隐形提款机”,三方共赢的底层逻辑
  • 从零到上线只需22分钟:VSCode低代码插件标准化配置流程(含安全审计+版本锁+灰度发布)
  • 重庆数据备份公司排行榜单
  • 把Snort当“网络监控摄像头”:5分钟教你用嗅探模式分析本地网络流量(Windows实操)
  • PCDViewer 5.3.0保姆级教程:从点云加载、渲染到地面滤波,手把手教你玩转SLAM数据可视化
  • 如何高效部署tts-vue离线语音合成工具:3个关键配置方案解决实际应用问题
  • 3分钟解锁英雄联盟全皮肤:R3nzSkin国服特供版终极指南
  • 告别手动秒杀:3步掌握京东自动化抢购脚本
  • Java智能地址解析:5大架构优势构建企业级数据治理方案
  • 从‘玄学’到科学:深度解读随机种子(seed)如何影响你的模型效果与调参策略
  • QMC2MP3终极指南:3步解锁QQ音乐格式限制,实现音乐自由
  • 抖音批量下载工具:如何高效管理你的内容素材库?
  • 从AutoCAD到激光切割:一份给创客的DXF文件避坑指南(含Cura/Inkscape配置)
  • 微信聊天记录永久保存:3步掌握WeChatMsg免费本地备份方案
  • 杰理之声道分配方式【篇】
  • 四足机器人运动控制:仿真训练与实战部署全解析
  • 远程仓库可能损坏?遇到‘Can‘t push refs to remote’错误的全链路诊断与修复指南
  • 机器学习不平衡分类评估指标全解析
  • 别再手动设规则了!用Altium Designer 20的规则导入/导出,5分钟搞定PCB布线预设
  • 第1集:面试官视角:AIOps 核心能力模型与面试项目全局搭建【免费试读】
  • RH850U2A内存布局实战:手把手教你规划Bootloader、APP与Data Flash(附栈溢出防护技巧)
  • STM32显示中文太占Flash?试试把字库放到SPI Flash里的高效方案
  • DBC文件不只是给CANoe用的:手把手教你用cantools把DBC转成C代码(附工程集成指南)
  • 立创EDA专业版3D模型导出避坑指南:如何完美适配Altium Designer 20(STEP文件处理详解)
  • 高效开源PDF处理实战指南:Windows平台Poppler工具深度解析
  • 抖音批量下载终极指南:3分钟掌握高效视频采集技巧
  • 抖音视频批量下载终极指南:开源工具免费下载无水印视频
  • Whisper语音识别实战:高效音频转文字技术解析
  • 华为ENSP实战:手把手教你配置多区域OSPF,并解决Area 0与非骨干区域互联的常见报错