当前位置:网站首页>leetcode:437. Path sum III [DFS selected or not selected?]
leetcode:437. Path sum III [DFS selected or not selected?]
2022-04-23 12:36:00 【Review of the white speed Dragon King】
analysis
Clear thinking
Select or not select the current
Choose the current one, take and enter the left and right children
Don't choose the current one and go directly to the left and right children
If you choose, go directly to the inner layer dfs Get it done
If not selected, the outer layer calls self
Get one with each node as start
Then choose the current judgment
ac 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) -> int:
def dfs(root, targetSum):
if not root:
return 0
ret = 0
if root.val == targetSum:
ret += 1
# Current selected root Of
ret += dfs(root.left, targetSum - root.val)
ret += dfs(root.right, targetSum - root.val)
return ret
if not root:
return 0
ret = dfs(root, targetSum)
# Not the current root
ret += self.pathSum(root.left, targetSum)
ret += self.pathSum(root.right, targetSum)
return ret
summary
Select the current to enter dfs, And then determine none, If you just wait +1
Otherwise, select the current to enter the next layer dfs
If you don't choose to enter directly from the outside pathSum, Ignore root.val The value of the can
Here is a classic Select the current and then traverse the left and right or If you don't select the current, you can traverse the left back
版权声明
本文为[Review of the white speed Dragon King]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231233448355.html
边栏推荐
- 论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
- Windows2008系统如何切换PHP版本
- uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程
- Zero trust in network information security
- How do programmers finalize nucleic acid statistics with 130 lines of code
- Basic software testing Day2 - Case Execution
- STM32工程移植:不同型号芯片工程之间的移植:ZE到C8
- Solution of asynchronous clock metastability -- multi bit signal
- box-sizing
- c# 设置logo图标和快捷方式的图标
猜你喜欢
Nativeformysql connects to MySQL 8 prompt: 1251 - client does not support authentication protocol
box-sizing
I changed to a programmer at the age of 31. Now I'm 34. Let me talk about my experience and some feelings
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
What are the forms of attack and tampering on the home page of the website
box-sizing
一个平面设计师的异想世界|ONES 人物
风尚云网学习-h5的input:type属性的image属性
Metalama简介4.使用Fabric操作项目或命名空间
Zero trust in network information security
随机推荐
Metalama简介4.使用Fabric操作项目或命名空间
一个平面设计师的异想世界|ONES 人物
SynchronousQueue 源码解析
The database navigator uses the default MySQL connection prompt: the server time zone value 'Ö Ð¹ ú±ê ×¼ ʱ ¼ ä’ is unrecognized or repres
论文解读(CGC)《CGC: Contrastive Graph Clustering for Community Detection and Tracking》
S2-062 remote command execution vulnerability recurrence (cve-2021-31805)
【vulnhub靶场】-dc2
Please help me see what this is, mysql5 5. Thanks
In idea Solution to the problem of garbled code in Chinese display of properties file
[unity note] basic lighting in l4unity
Lesson 26 static member functions of classes
对话PostgreSQL作者Bruce:“转行”是为了更好地前行
实现一个盒子在父盒子中水平垂直居中的几种“姿势”
Idea database navigator plug-in
解决disagrees about version of symbol device_create
uni-app 原生APP-本地打包集成极光推送(JG-JPUSH)详细教程
【每日一题】棋盘问题
Pre competition practice of TIANTI competition
传统企业如何应对数字化转型?这些书给你答案
Fastjson 2 来了,性能继续提升,还能再战十年