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

[豪の算法奇妙冒险] 代码随想录算法训练营第十六天 | 513-找树左下角的值、112-路径总和、113-路径总和Ⅱ、106-从中序与后序遍历序列构造二叉树、105-从前序与中序遍历序列构造二叉树

代码随想录算法训练营第十六天 | 513-找树左下角的值、112-路径总和、113-路径总和Ⅱ、106-从中序与后序遍历序列构造二叉树、105-从前序与中序遍历序列构造二叉树


LeetCode513 找树左下角的值

题目链接:https://leetcode.cn/problems/find-bottom-left-tree-value/description/

文章讲解:https://programmercarl.com/0513.找树左下角的值.html

视频讲解:https://www.bilibili.com/video/BV1424y1Z7pn/?vd_source=b989f2b109eb3b17e8178154a7de7a51

​ 这题要找的是最后一行的最左边节点的值,其实就是要找到深度最深的最左边的叶子节点

​ 需要类里的两个全局变量,maxDepth用来记录最大深度,result记录最大深度最左节点的数值,优先遍历左节点,确保是最左边

​ 当遇到叶子节点时,统计当前深度,若比maxDepth记录值大则更新result为当前节点数值,注意在找最大深度的时候,递归过程中依然要使用回溯(递归回来上一层的时候,需要再把depth减一)

image-20251207183059004

class Solution {int result = 0;int maxDepth = Integer.MIN_VALUE;public int findBottomLeftValue(TreeNode root) {getResult(root,1);return result;}public void getResult(TreeNode node, int depth){if(node.left == null && node.right == null){if(depth > maxDepth){result = node.val;maxDepth = depth;}return;}if(node.left != null){depth++;getResult(node.left, depth);depth--;}if(node.right != null){depth++;getResult(node.right, depth);depth--;}}
}

​ 若使用迭代法做这题,就是采用层序遍历,每一层记录最开始遍历的那个节点的值,即为最左,遍历到最后一层即为最左下角的值

image-20251207184535807

class Solution {public int findBottomLeftValue(TreeNode root) {int result = 0;if(root == null){return result;}Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);while(!queue.isEmpty()){int size = queue.size();for(int i = 0; i < size; i++){TreeNode curNode = queue.poll();if(i == 0){result = curNode.val;}if(curNode.left != null){queue.offer(curNode.left);}if(curNode.right != null){queue.offer(curNode.right);}}}return result;}
}

LeetCode112 路径总和

题目链接:https://leetcode.cn/problems/path-sum/description/

文章讲解:https://programmercarl.com/0112.路径总和.html

视频讲解:https://www.bilibili.com/video/BV19t4y1L7CR/?vd_source=b989f2b109eb3b17e8178154a7de7a51

​ 累加然后判断是否等于目标和,代码会比较麻烦,可以用递减,让计数器count初始为目标和,然后每次减去遍历路径节点上的数值。若最后count == 0且到达叶子节点的话,说明找到了目标和、目标路径;若遍历到了叶子节点,但count不为0,就是没找到

​ 如果递归函数返回true,说明找到了合适的路径,直接返回true

image-20251207192335916

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

LeetCode113 路径总和Ⅱ

题目链接:https://leetcode.cn/problems/path-sum-ii/description/

image-20251207195438909

​ 这题注意收割结果的时候是 result.add(new ArrayList<>(records)); ,因为直接写了 result.add(records); 而DEBUG了很久(捂脸)

class Solution {public List<List<Integer>> pathSum(TreeNode root, int targetSum) {List<List<Integer>> result = new ArrayList<>();if(root == null){return result;}List<Integer> records = new ArrayList<>();getPath(root, targetSum, records, result);return result;}public void getPath(TreeNode node, int count, List<Integer> records, List<List<Integer>> result){records.add(node.val);count -= node.val;if(node.left == null && node.right == null){if(count == 0){result.add(new ArrayList<>(records));}return;}if(node.left != null){getPath(node.left, count, records, result);records.remove(records.size()-1);}if(node.right != null){getPath(node.right, count, records, result);records.remove(records.size()-1);}}
}

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

题目链接:https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/

文章讲解:https://programmercarl.com/0106.从中序与后序遍历序列构造二叉树.html

视频讲解:https://www.bilibili.com/video/BV1vW4y1i7dn/

​ 思想还算明了,但是代码实现起来很多细节需要处理,还是比较难的

​ 第一步:若数组大小为0,说明为空节点,返回null

​ 第二步:若不为空,则取后序数组最后一个元素作为节点元素

​ 第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点index

​ 第四步:切割中序数组,切成中序左数组和中序右数组

​ 第五步:切割后序数组,切成后序左数组和后序右数组

​ 第六步:递归处理当前节点的左区间和右区间

​ 切割时始终遵循左闭右开的原则,先处理中序数组,中序左数组范围为(inorder.begin ~ inorder.begin + index),中序右数组范围为(inorder.begin + index + 1 ~ inorder.end)

​ 接着处理后序数组,后序左数组范围为(postorder.begin ~ postorder.begin + 中序左数组大小),后序右数组范围为(postorder.begin + 中序左数组大小 ~ postorder.end - 1)

​ 切割完以后就是接着往下递归,当前节点左右孩子

image-20251207205935468

class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {if(postorder.length == 0 || inorder.length == 0){return null;}return build(inorder, 0, inorder.length, postorder, 0, postorder.length);}public TreeNode build(int[] inorder, int inLeft, int inRight, int[] postorder, int postLeft, int postRight){if(inLeft == inRight){return null;}int value = postorder[postRight-1];TreeNode node = new TreeNode(value);int index = 0;for(; index < inRight; index++){if(inorder[index] == value){break;}}int leftInLeft = inLeft;int leftInRight = index;int rightInLeft = index + 1;int rightInRight = inRight;int leftPostLeft = postLeft;int leftPostRight = postLeft + (index - inLeft);int rightPostLeft = leftPostRight;int rightPostRight = postRight - 1;node.left = build(inorder, leftInLeft, leftInRight, postorder, leftPostLeft, leftPostRight);node.right = build(inorder, rightInLeft, rightInRight, postorder, rightPostLeft, rightPostRight);return node;}
}

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

题目链接:https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/

image-20251207212713072

class Solution {public TreeNode buildTree(int[] preorder, int[] inorder) {if(preorder.length == 0 || inorder.length == 0){return null;}return build(preorder, 0, preorder.length, inorder, 0, inorder.length);}public TreeNode build(int[] preorder, int preLeft, int preRight, int[] inorder, int inLeft, int inRight){if(preLeft == preRight){return null;}int value = preorder[preLeft];TreeNode node = new TreeNode(value);if(preRight - preLeft == 1){return node;}int index = 0;for(; index < inRight; index++){if(inorder[index] == value){break;}}int leftInLeft = inLeft;int leftInRight = index;int rightInLeft = index + 1;int rightInRight = inRight;int leftPreLeft = preLeft + 1;int leftPreRight = leftPreLeft + (index - inLeft);int rightPreLeft = leftPreRight;int rightPreRight = preRight;node.left = build(preorder, leftPreLeft, leftPreRight, inorder, leftInLeft, leftInRight);node.right = build(preorder, rightPreLeft, rightPreRight, inorder, rightInLeft, rightInRight);return node;}
}
http://www.jsqmd.com/news/65674/

相关文章:

  • 20232407 2025-2026-1 《网络与系统攻防技术》 实验八实验报告
  • 北京陪诊服务专业排行榜出炉,守嘉、翌家、华夏天和位居三甲
  • 【SPI】SPI与QSPI异同与使用
  • leetcode57. 插入区间
  • Linux 运维100 条命令
  • 个人电脑上的本地私有知识库解决方案:访答知识库深度解析
  • Spark-3.5.7文档1 - 快捷开始
  • 北京上门收画回收名家字画机构公司推荐和排行
  • 2025.12.7——1蓝
  • 虚拟机设置网络适配器为桥接模式,并且设置固定ip
  • 北京上门收字画机构推荐榜单
  • 洛谷P3287 [SCOI2014] 方伯伯的玉米田 (二维树状数组+dp枚举)
  • ES2T 34托盘相关报警
  • 某机构推出AI模型深度定制服务,重塑品牌专属生成式AI
  • Nano-vLLM-Ascend
  • 20251207 之所思 - 人生如梦
  • 2025NOIP游记(有空更新)
  • 【2025年12月最新】英语四级历年真题试卷、听力音频及答案解析~PDF电子版(2015-2025年6月) - 详解
  • 不同深度学习框架中实现人工神经元基本计算单元的模块对比
  • [容器] Podman : 一款新型的容器引擎与容器管理工具
  • 从0构建深度学习框架——揭秘深度学习框架的黑箱
  • SVPWM基础
  • JDK的安装与删除
  • C语言字符串函数学习 - hillo
  • 实用工具:担心腾讯ACE把你的硬盘扫坏了?用DiskGenius一分钟检测硬盘是否损坏
  • 百度之星 2025 游记
  • 北京上门收酒服务权威推荐榜,四家机构获评优质服务商
  • 20232406 2024-2025-1 《网络与系统攻防技术》实验八实验报告
  • Win10最终版下载 d485系统站
  • AI元人文构想全维解读:从意义行为原生到文明共生体