当前位置:网站首页>【leetcode】112. Path sum - 113 Path sum II
【leetcode】112. Path sum - 113 Path sum II
2022-06-26 15:37:00 【liiiiiiiiiiiiike】
For details, please refer to 112. The sum of the paths
For details, please refer to 113. The sum of the paths II
112. The sum of the paths
Their thinking :
- The first sequence traversal , If recursive root It's empty , Just go back to False Indicates that there is no and in this path TargetSum
- If the left and right subtrees are empty , And the current node is equal to targetSum, Just go back to True
- Then recursive left and right subtrees , See if the left and right subtrees can be matched
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 # No, root, I just can't find
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. The sum of the paths II
Their thinking :
- Traversal by preorder
- The current node is the leaf node , And the value of the current node == targetsum, Save this path
- If there is a left subtree, go to the left subtree , The left and right subtrees have been traversed , Will path Current node in pop, For example, the left node has been found , Output the left node path, Right node in 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
边栏推荐
- Binding method of multiple sub control signal slots under QT
- Unity C # e-learning (VIII) -- www
- 一篇博客彻底掌握:粒子滤波 particle filter (PF) 的理论及实践(matlab版)
- Learning memory barrier
- JS handwritten bind, apply, call
- 评价——TOPSIS
- Comparative analysis of restcloud ETL and kettle
- 刷题笔记(十九)--二叉树:二叉搜索树的修改与构造
- Unity unitywebrequest download package
- 音视频学习(一)——PTZ控制原理
猜你喜欢

小程序:uniapp解决 vendor.js 体积过大的问题
![[tcapulusdb knowledge base] tcapulusdb system user group introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb system user group introduction
![[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing](/img/b6/d288065747425863b9af95ec6fd554.png)
[file] VFS four structs: file, dentry, inode and super_ What is a block? difference? Relationship-- Editing

数据库-视图

Particle filter PF -- Application in maneuvering target tracking (particle filter vs extended Kalman filter)

Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved

在校生学习生涯总结(2022)

Unable to download Plug-in after idea local agent

Smoothing data using convolution

Comparative analysis of restcloud ETL and kettle
随机推荐
Mr. Du said that the website was updated with illustrations
JS handwritten bind, apply, call
JS events
JS simple deepcopy (Introduction recursion)
Comparative analysis of restcloud ETL and kettle
效率超级加倍!pycharm十个小技巧就是这么神
【TcaplusDB知识库】TcaplusDB数据构造介绍
There are so many vulnerabilities in tcp/ip protocol?
[C language practice - printing hollow upper triangle and its deformation]
[applet practice series] Introduction to the registration life cycle of the applet framework page
Inaccurate data accuracy in ETL process
Notes on brushing questions (19) -- binary tree: modification and construction of binary search tree
买股票通过券商经理的开户二维码开户资金是否安全?想开户炒股
Audio and video learning (I) -- PTZ control principle
评价——TOPSIS
[CEPH] Introduction to cephfs caps
TCP/IP协议竟然有这么多漏洞?
[tcapulusdb knowledge base] Introduction to tcapulusdb data structure
MySQL数据库基本SQL语句教程之高级操作
Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)