当前位置:网站首页>【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
边栏推荐
- [CEPH] Introduction to cephfs caps
- How to load the contour CAD drawing of the engineering coordinate system obtained by the designer into the new earth
- selenium将元素保存为图片
- [CEPH] cephfs internal implementation (I): Concept -- undigested
- [wechat applet] event binding, do you understand?
- [CEPH] MKDIR | mksnap process source code analysis | lock state switching example
- 整理了一批脚本标准的函数模块(2021版)
- Using restcloud ETL shell component to schedule dataX offline tasks
- Function: crypto JS encryption and decryption
- 学习内存屏障
猜你喜欢

High frequency interview 𞓜 Flink Shuangliu join

Using restcloud ETL shell component to schedule dataX offline tasks

Function: crypto JS encryption and decryption

面试高频 | 你追我赶的Flink双流join

音视频学习(一)——PTZ控制原理

在重新格式化时不要删除自定义换行符(Don‘t remove custom line breaks on reformat)
Advanced operation of MySQL database basic SQL statement tutorial

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

数据库-视图

AbortController的使用
随机推荐
selenium chrome 禁用js 禁用图片
Learning memory barrier
【毕业季·进击的技术er】 什么是微信小程序,带你推开小程序的大门
Database - integrity constraints
Lexin AWS IOT expresslink module achieves universal availability
vsomeip3 双机通信文件配置
[graduation season · advanced technology Er] what is a wechat applet, which will help you open the door of the applet
2022北京石景山区专精特新中小企业申报流程,补贴10-20万
Ansible自动化的运用
JS之手写 bind、apply、call
[tcapulusdb knowledge base] tcapulusdb doc acceptance - transaction execution introduction
【文件】VFS四大struct:file、dentry、inode、super_block 是什么?区别?关系?--编辑中
How to handle 2gcsv files that cannot be opened? Use byzer
IDEA本地代理后,无法下载插件
音视频学习(二)——帧率、码流和分辨率
[CEPH] cephfs internal implementation (IV): how is MDS started-- Undigested
One click analysis hardware /io/ national network performance script (strong push)
一键安装gcc脚本
MongoDB系列之适用场景和不适用场景
音视频学习(三)——sip协议