当前位置:网站首页>小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
小黑leetcode清爽雨天之旅,刚吃完宇飞牛肉面、麻辣烫和啤酒:112. 路径总和
2022-08-09 21:35:00 【小黑无敌】
小黑自己的解法
# 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:
self.flag = False
if not root:
return self.flag
def list_path(root,sum_):
sum_ = root.val + sum_
# 判断是否到达叶子结点
if (not root.left) and (not root.right):
#print(sum_)
if sum_ == targetSum:
self.flag = True
if root.left and not self.flag :
list_path(root.left,sum_)
if root.right and not self.flag:
list_path(root.right,sum_)
list_path(root,0)
return self.flag
递归法
# 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
if (not root.left and not root.right):
return targetSum == root.val
return self.hasPathSum(root.left,targetSum - root.val) or self.hasPathSum(root.right,targetSum - root.val)
广度优先搜索
# 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
q_node = deque([root])
q_num = deque([root.val])
# 队列操作
while q_node:
node = q_node.popleft()
num = q_num.popleft()
# node为叶子结点
if (not node.left) and (not node.right):
if num == targetSum:
return True
continue
if node.left:
q_node.append(node.left)
q_num.append(num + node.left.val)
if node.right:
q_node.append(node.right)
q_num.append(num + node.right.val)
return False
边栏推荐
- cadence中复制一部分PCB到另一个PCB中去
- How to fix Windows 11 not finding files
- Redis 大的情况下,key 要如何处理?
- hdu 3341 Lost's revenge(dp+Ac自动机)
- Can I make a TCP connection without accept?
- Optimization of SQL Statements and Indexes
- [Essay] To the friends of the 19th issue
- Overview of Security Analysis Technology for Smart Home Devices
- Beat the interviewer, the CURD system can also make technical content
- Photometric Stereo 光度立体法三维重建
猜你喜欢
没有 accept,我可以建立 TCP 连接吗?
Word第一页不要页眉怎么设置?设置Word首页不要页眉方法教程
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰二氧化硅颗粒用
Simulation of Water Temperature Control System Based on Fuzzy PID Controller
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified silica particles
数据集成API如何成为企业数字化转型的关键?
Word怎么设置图片衬于文字下方?两种方法教你设置Word图片衬于文字下方
CMake 安装升级更高版本
普源精电上半年扭亏为盈,高端产品持续发力!你看好仪器界“华为”吗?
Ankerui supports Ethernet communication, profibus communication embedded energy meter APM guiding technical requirements-Susie Week
随机推荐
Cholesterol-PEG-Thiol, CLS-PEG-SH, Cholesterol-PEG-Sulfhydryl for improved solubility
阿里二面:没有 accept,能建立 TCP 连接吗?
XXE-XML外部实体注入-知识点
字节一面:TCP 和 UDP 可以使用同一个端口吗?
Daily practice of PMP | Do not get lost in the exam -8.8 (including agility + multiple choice)
Unity2D_线框材质
Ankerui supports Ethernet communication, profibus communication embedded energy meter APM guiding technical requirements-Susie Week
Optimization of SQL Statements and Indexes
筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
Word怎么设置图片衬于文字下方?两种方法教你设置Word图片衬于文字下方
tki-tree 树组件控制默认展开第几层数据
基于光通信的6G水下信道建模综述
Hessian Matrix 海森矩阵
Don't use array.length-1 to get the last item of the array
Win11搜索不到文件的解决方法
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷修饰二氧化硅颗粒用
Interviewer: How to deal with Redis big key?
Puyuan Jingdian turned losses into profits in the first half of the year, and high-end products continued to develop!Are you optimistic about "Huawei" in the instrument industry?
数据集成API如何成为企业数字化转型的关键?
【Efficient Tools】Remote Control Software ToDesk (Favorites)