当前位置:网站首页>【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
边栏推荐
- Unity C # e-learning (10) -- unitywebrequest (1)
- Audio and video learning (III) -- SIP protocol
- High frequency interview 𞓜 Flink Shuangliu join
- 【ceph】mkdir|mksnap流程源码分析|锁状态切换实例
- About selenium common. exceptions. Webdriverexception: message: an unknown server side error solution (resolved)
- Vsomeip3 dual computer communication file configuration
- Function: crypto JS encryption and decryption
- 【ceph】CephFS 内部实现(四):MDS是如何启动的?--未消化
- JS之事件
- Restcloud ETL extracting dynamic library table data
猜你喜欢
![[tcapulusdb knowledge base] tcapulusdb OMS business personnel permission introduction](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[tcapulusdb knowledge base] tcapulusdb OMS business personnel permission introduction

刷题笔记(十九)--二叉树:二叉搜索树的修改与构造

评价——TOPSIS

PCIe Capabilities List

【TcaplusDB知识库】TcaplusDB常规单据介绍
![[CEPH] cephfs internal implementation (I): Concept -- undigested](/img/5c/ca666118848b4f3042b834fb79d27f.png)
[CEPH] cephfs internal implementation (I): Concept -- undigested

【问题解决】新版webots纹理等资源文件加载/下载时间过长
![[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction](/img/7c/25a88f46e02cebd2e003b9590b9c13.png)
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction

High frequency interview 𞓜 Flink Shuangliu join

效率超级加倍!pycharm十个小技巧就是这么神
随机推荐
【leetcode】48.旋转图像
Restcloud ETL extracting dynamic library table data
【ceph】CEPHFS 内部实现(一):概念篇--未消化
PHP file upload 00 truncation
SQLite loads CSV files and performs data analysis
Binding method of multiple sub control signal slots under QT
【毕业季·进击的技术er】 什么是微信小程序,带你推开小程序的大门
【TcaplusDB知识库】TcaplusDB单据受理-创建业务介绍
查词翻译类应用使用数据接口api总结
Unity C # e-learning (10) -- unitywebrequest (2)
一键分析硬件/IO/全国网络性能脚本(强推)
Summer camp is coming!!! Chongchongchong
Sikuli automatic testing technology based on pattern recognition
【SNMP】snmp trap 介绍、安装、命令|Trap的发送与接收代码实现
One click analysis hardware /io/ national network performance script (strong push)
Use of abortcontroller
一篇博客彻底掌握:粒子滤波 particle filter (PF) 的理论及实践(matlab版)
【TcaplusDB知识库】TcaplusDB常规单据介绍
夏令营来啦!!!冲冲冲
[CEPH] MKDIR | mksnap process source code analysis | lock state switching example