当前位置:网站首页>【leetcode】112. 路径总和 - 113. 路径总和 II
【leetcode】112. 路径总和 - 113. 路径总和 II
2022-06-26 15:21:00 【liiiiiiiiiiiiike】
题目详见112. 路径总和
题目详见 113. 路径总和 II
112.路径总和
解题思路:
- 先序遍历,如果递归中root 为空,就返回False表示该路径下没有和为TargetSum
- 如果左右子树都为空,并且当前节点等于targetSum,就返回True
- 然后递归左右子树,看左右子树是否能被匹配到
talk is cheap , show me the code
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
if not root: return False # 没有root,就是没有找到
if not root.left and not root.right and root.val == targetSum:
return True
return self.hasPathSum(root.left,targetSum-root.val) or self.hasPathSum(root.right,targetSum-root.val)
113.路径总和 II
解题思路:
- 用先序遍历
- 当前节点为叶子节点,并且当前节点的值== targetsum,则将此条路径保存
- 如果有左子树则去左子树上找,左右子树都遍历完了,则将path中当前节点pop,例如左节点找完了,将左节点出path,右节点入path
Talk is cheap, show me the code
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
if not root:
return []
path = []
result = []
def dfs(root,targetSum):
if not root:
return
path.append(root.val)
targetSum -= root.val
if not root.left and not root.right and targetSum==0:
result.append(path[:])
if root.left:
dfs(root.left,targetSum)
if root.right:
dfs(root.right,targetSum)
path.pop()
dfs(root,targetSum)
return result
边栏推荐
- [tcapulusdb knowledge base] tcapulusdb operation and maintenance doc introduction
- [tcapulusdb knowledge base] tcapulusdb system user group introduction
- [graduation season · advanced technology Er] what is a wechat applet, which will help you open the door of the applet
- 刷题笔记(十九)--二叉树:二叉搜索树的修改与构造
- vsomeip3 双机通信文件配置
- Don't remove custom line breaks on reformat
- 2022北京石景山区专精特新中小企业申报流程,补贴10-20万
- [C language practice - printing hollow upper triangle and its deformation]
- 数据库-视图
- [tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
猜你喜欢

音视频学习(三)——sip协议

Summary of students' learning career (2022)

# 粒子滤波 PF——三维匀速运动CV目标跟踪(粒子滤波VS扩展卡尔曼滤波)

在重新格式化时不要删除自定义换行符(Don‘t remove custom line breaks on reformat)

【TcaplusDB知识库】TcaplusDB运维单据介绍

SQLite loads CSV files and performs data analysis

How to handle 2gcsv files that cannot be opened? Use byzer

Smoothing data using convolution
![[CEPH] Lock Notes of cephfs](/img/9a/b68e7b07b1187794c0dbed36eea749.png)
[CEPH] Lock Notes of cephfs
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction](/img/66/f3ab0514d691967ad88535ae1448c1.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - table creation approval introduction
随机推荐
MySQL数据库基本SQL语句教程之高级操作
SQLite loads CSV files and performs data analysis
北京房山区专精特新小巨人企业认定条件,补贴50万
[CEPH] cephfs internal implementation (I): Concept -- undigested
【TcaplusDB知识库】TcaplusDB单据受理-建表审批介绍
vue中缓存页面 keepAlive使用
Compile configuration in file
Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)
Cache page keepalive use in Vue
[tcapulusdb knowledge base] tcapulusdb system user group introduction
HR export data Excel VBA
[CEPH] Introduction to cephfs caps
【ceph】CephFS 内部实现(二):示例--未消化
【ceph】mkdir|mksnap流程源码分析|锁状态切换实例
Unity unitywebrequest download package
[wechat applet] event binding, do you understand?
Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)
音视频学习(一)——PTZ控制原理
【C语言练习——打印空心上三角及其变形】
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure