当前位置:网站首页>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
边栏推荐
- SQLserver怎么插入或更新当天的星期数,bit而不是文本
- Windows2008系统如何切换PHP版本
- 网络信息安全之零信任
- Qt一个进程运行另一个进程
- 风尚云网学习-input属性总结
- AI video cloud vs narrowband HD, who is the darling of the video era
- 为什么hash%length==hash&(length-1)的前提是 length 是 2 的 n 次方
- 甲辰篇 創世紀《「內元宇宙」聯載》
- Lesson 24 analysis of classical problems
- Jiachen chapter Genesis "inner universe" joint Edition
猜你喜欢
How to prevent the website from being hacked and tampered with
QT draw image
c# 设置logo图标和快捷方式的图标
The maximum number of remote desktop servers has been exceeded
网络信息安全之零信任
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
Idea setting copyright information
NPDP|产品经理如何做到不会被程序员排斥?
基于卷积神经网络的遥感影像分类识别系统
随机推荐
Plato Farm-以柏拉图为目标的农场元宇宙游戏
C set Logo Icon and shortcut icon
Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
宝塔面板命令行帮助教程(包含重置密码)
为什么hash%length==hash&(length-1)的前提是 length 是 2 的 n 次方
BUUCTF WEB [GXYCTF2019]禁止套娃
SQL 练习(一)
AI 视频云 VS 窄带高清,谁是视频时代的宠儿
Pre competition practice of TIANTI competition
Idea code quality specification plug-in sonarlint
php生成json处理中文
风尚云网学习-h5的input:type属性的image属性
STM32CubeProgrammer基础使用说明
The database navigator uses the default MySQL connection prompt: the server time zone value 'Ö Ð¹ ú±ê ×¼ ʱ ¼ ä’ is unrecognized or repres
S2-062 remote command execution vulnerability recurrence (cve-2021-31805)
洛谷P3236 [HNOI2014]画框 题解
S2-062 远程命令执行漏洞复现(cve-2021-31805)
使用Source Insight查看编辑源代码
Why is there a wrapper class? By the way, how to convert basic data types, wrapper classes and string classes?