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

python coding with ChatGPT 打卡第17天| 二叉树:找树左下角的值、路径总和

相关推荐
python coding with ChatGPT 打卡第12天| 二叉树:理论基础
python coding with ChatGPT 打卡第13天| 二叉树的深度优先遍历
python coding with ChatGPT 打卡第14天| 二叉树的广度优先遍历
python coding with ChatGPT 打卡第15天| 二叉树:翻转二叉树、对称二叉树
python coding with ChatGPT 打卡第16天| 二叉树:完全二叉树、平衡二叉树、二叉树的所有路径、左叶子之和

文章目录

  • 找树左下角的值
    • Key Points
    • 相关题目
    • 视频讲解
    • 重点分析
  • 路径总和
    • Key Points
    • 相关题目
    • 视频讲解
    • 重点分析

找树左下角的值

Key Points

找出树的最后一行的最左边的值

相关题目

513. 找树左下角的值

视频讲解

递归中带着回溯

重点分析

方法一:层序遍历

def findBottomLeftValue(root):queue_record = [root]res = root.valwhile queue_record:level_size = len(queue_record)for i in range(level_size):node = queue_record.pop(0)if i==0:res = node.valif node.left:queue_record.append(node.left)if node.right:queue_record.append(node.right)return res

方法二:层序遍历简洁版

class Solution(object):def findBottomLeftValue(self, root):if not root:return Nonequeue = [root]while queue:current = queue.pop(0)# 先右后左加入队列,确保左边的节点最后被处理,从而保留在current中if current.right:queue.append(current.right)if current.left:queue.append(current.left)# 循环结束时,current中存储的是最后一层最左边的节点return current.val

这段代码使用了BFS来确保按层遍历树的节点,并且通过在每层遍历时记录遍历到的第一个节点值,最终找到了最后一行最左边的值。请注意,这里故意先将右子节点加入队列,然后加入左子节点,是为了在处理每一层的节点时,最后处理左子节点,但是对于寻找最后一行最左边的值的目的而言,只需要记录每一层第一次访问的节点即可,因此实际上你可以按照正常的顺序(先左后右)加入队列,然后最后处理的节点即为所求。这样的处理方式更直观且易于理解。

方法三:递归法

class Solution:def findBottomLeftValue(self, root: TreeNode) -> int:self.max_depth = float('-inf')self.result = Noneself.traversal(root, 0)return self.resultdef traversal(self, node, depth):if not node.left and not node.right:if depth > self.max_depth:self.max_depth = depthself.result = node.valreturnif node.left:self.traversal(node.left, depth+1)if node.right:self.traversal(node.right, depth+1)

递归的另一种写法,由ChatGPT提供
在这里插入图片描述

路径总和

Key Points

叶子节点是指没有子节点的节点。

相关题目

112. 路径总和
113. 路径总和ii

视频讲解

路径总和

重点分析

112
方法一:递归

def hasPathSum(root: TreeNode, targetSum: int) -> bool:if not root:return False# 更新目标和targetSum -= root.val# 如果是叶子节点,检查目标和是否为0if not root.left and not root.right:return targetSum == 0# 递归遍历左右子节点return hasPathSum(root.left, targetSum) or hasPathSum(root.right, targetSum)

在这里插入图片描述方法二:迭代法

def hasPathSum(root, targetSum):if not root:return Falsestack_record = [(root, root.val)]while stack_record:node, value = stack_record.pop()if not node.left and not node.right:if value == targetSum:return Trueelse:if node.right:stack_record.append((node.right, value+node.right.val))if node.left:stack_record.append((node.left, value + node.left.val))return False

113 方法一:递归法
在这里插入图片描述

class Solution:def pathSum(self, root: TreeNode, targetSum: int) -> [[int]]:result = []self.dfs(root, targetSum, [], result)return resultdef dfs(self, node, targetSum, path, result):if not node:return# 添加当前节点到路径path.append(node.val)# 检查是否是叶子节点且路径总和等于目标和if not node.left and not node.right and sum(path) == targetSum:result.append(list(path))else:# 递归遍历左右子节点self.dfs(node.left, targetSum, path, result)self.dfs(node.right, targetSum, path, result)# 回溯前去除当前节点path.pop()# 示例使用
# 假设有一个二叉树和目标和,可以创建TreeNode实例并调用Solution().pathSum(root, targetSum)来获取结果

在这里插入图片描述

方法二:迭代法

def pathSum(root, targetSum):if not root:return []stack_record = [(root, [root.val])]res = []while stack_record:node, value_list = stack_record.pop()if not node.left and not node.right:if sum(value_list) == targetSum:res.append(value_list)else:if node.right:stack_record.append((node.right, value_list+[node.right.val]))if node.left:stack_record.append((node.left, value_list + [node.left.val]))return res
http://www.lryc.cn/news/295254.html

相关文章:

  • 2020年通信工程师初级 综合能力 真题
  • 12.0 Zookeeper 数据同步流程
  • 作业2.6
  • Qt应用软件【协议篇】TCP示例
  • C# CAD交互界面-自定义面板集(四)
  • 物流自动化移动机器人|HEGERLS三维智能四向穿梭车助力优化企业供应链
  • EasyExcel下载带下拉框和批注模板
  • C语言之字符逆序(牛客网)
  • RAPTOR:树组织检索的递归抽象处理
  • 图论:合适的环
  • 【数据分享】1929-2023年全球站点的逐月平均降水量(Shp\Excel\免费获取)
  • React+Antd实现省、市区级联下拉多选组件(支持只选省不选市)
  • CentOS镜像如何下载?在VMware中如何安装?
  • 计算机科学导论(4)DMA传输原理
  • select、poll和epoll的区别
  • gpt今日最新新闻:gpts的广泛应用
  • 【进入游戏行业选游戏特效还是技术美术?】
  • (delphi11最新学习资料) Object Pascal 学习笔记---第4章第2.3节(常量参数)
  • 事件在状态流程图中的工作方式
  • 幻兽帕鲁能在Mac上运行吗?幻兽帕鲁Palworld新手攻略
  • elementPlus实现动态表格单元格合并span-method方法总结
  • 视频上传 - 断点续传那点事
  • Scala 和 Java在继承机制方面的区别
  • spark sql上线前的调试工作实现
  • java -jar启动SpringBoot项目时配置文件加载位置与优先级
  • 每日一题 力扣LCP30.魔塔游戏
  • iPhone搞机记录
  • Linux中共享内存(mmap函数的使用)
  • Golang与Erlang有什么差异
  • cesium系列篇:Entity vs Primitive 源码解析(从Entity到Primitive)02