当前位置:网站首页>【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
边栏推荐
- 小程序:uniapp解决 vendor.js 体积过大的问题
- Use of abortcontroller
- 【TcaplusDB知识库】TcaplusDB系统用户组介绍
- 【leetcode】701. 二叉搜索树中的插入操作
- 反射修改final
- JS之手写 bind、apply、call
- 10 minutes to understand bim+gis fusion, common BIM data formats and characteristics
- selenium chrome 禁用js 禁用图片
- # 粒子滤波 PF——三维匀速运动CV目标跟踪(粒子滤波VS扩展卡尔曼滤波)
- MySQL数据库基本SQL语句教程之高级操作
猜你喜欢

10 minutes to understand bim+gis fusion, common BIM data formats and characteristics

Evaluate:huggingface detailed introduction to the evaluation index module

数据库-视图

Inaccurate data accuracy in ETL process

【TcaplusDB知识库】TcaplusDB单据受理-事务执行介绍

English语法_形容词/副词3级 - 原级句型

Particle filter PF - 3D CV target tracking with uniform motion (particle filter vs extended Kalman filter)

HW安全响应

【ceph】cephfs caps简介

Using restcloud ETL shell component to schedule dataX offline tasks
随机推荐
在重新格式化时不要删除自定义换行符(Don‘t remove custom line breaks on reformat)
RestCloud ETL抽取動態庫錶數據實踐
PCIe Capabilities List
反射修改final
shell脚本多进程并发写法实例(高阶修炼)
北京房山区专精特新小巨人企业认定条件,补贴50万
[tcapulusdb knowledge base] tcapulusdb OMS business personnel permission introduction
Is it safe to buy stocks and open accounts through the QR code of the securities manager? Want to open an account for stock trading
5 figures illustrate the container network
Keil4 opens the single-chip microcomputer project to a blank, and the problem of 100% program blocking of cpu4 is solved
vsomeip3 双机通信文件配置
Mr. Du said that the website was updated with illustrations
[CEPH] Lock Notes of cephfs
Summary of students' learning career (2022)
如何配置使用新的单线激光雷达
Audio and video learning (III) -- SIP protocol
2022北京石景山区专精特新中小企业申报流程,补贴10-20万
Restcloud ETL resolves shell script parameterization
Compile configuration in file
Lexin AWS IOT expresslink module achieves universal availability