leetcode python3 简单题112. Path Sum
1.编辑器
我使用的是win10+vscode+leetcode+python3
环境配置参见我的博客:
链接
2.第一百一十二题
(1)题目
英文:
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
Note: A leaf is a node with no children.
中文:
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
说明: 叶子节点是指没有子节点的节点。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/path-sum
(2)解法
① 循环法
(耗时:52ms,内存:15.7M)
class Solution:def hasPathSum(self, root: TreeNode, sum: int) -> bool:if not root:return Falsestack = [(root,sum)]while stack:node,val = stack.pop()if not node.left and not node.right and node.val == val:return Trueif node.right:stack.append((node.right,val-node.val))if node.left:stack.append((node.left,val-node.val))return False
② DFS
(耗时:44ms,内存:15.5M)
class Solution:def hasPathSum(self, root: TreeNode, sum: int) -> bool:if not root: return Falseif not root.left and not root.right and sum - root.val == 0:return Truereturn self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
leetcode python3 简单题112. Path Sum相关推荐
- leetcode python3 简单题167. Two Sum II - Input array is sorted
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百六十七题 (1)题目 英文: Given an array of intege ...
- leetcode python3 简单题1.Two Sum
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一题 (1)题目 英文: Given an array of integers, ...
- leetcode python3 简单题53. Maximum Subarray
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第五十三题 (1)题目 英文: Given an integer array num ...
- leetcode python3 简单题14. Longest Common Prefix
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第十四题 (1)题目 英文: Write a function to find th ...
- leetcode python3 简单题225. Implement Stack using Queues
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百二十五题 (1)题目 英文: Implement the following ...
- leetcode python3 简单题204. Count Primes
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零四题 (1)题目 英文: Count the number of prime ...
- leetcode python3 简单题202. Happy Number
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二百零二题 (1)题目 英文: Write an algorithm to det ...
- leetcode python3 简单题171. Excel Sheet Column Number
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百七十一题 (1)题目 英文: Given a column title as ...
- leetcode python3 简单题118. Pascal's Triangle
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十八题 (1)题目 英文: Given a non-negative int ...
最新文章
- 无人驾驶中的 3D 目标检测技术
- struts2学习:配置篇值请求处理元素
- 法律应是站在加密和隐私这面的……
- Linux下undefined reference to ‘pthread_create’问题解决
- C#教程5:操作算子(1)
- 图的遍历——深度优先搜索+广度优先搜索
- python print(chr(65))_python 内置函数
- oracle存储过程备份,Oracle存储过程(二)
- 超长正整数的加法(酷勤网)
- Linux学习简单教程和常用命令(小白学习法)
- C#实现Socket
- gmsl摄像头Android平台调试思路
- opencv java 车牌定位_用opencv实现在图像找到车牌号并检测出车牌号
- 动态规划—钢管切割问题
- Win11系统.NET Framework 3.5怎么启用?
- 基于SpringBoot的社区小型图书管理系统的设计与实现
- 慢SQL!压垮团队的最后一根稻草!
- apache2+php 初配置
- Restful中动词的使用
- window下安装wamp环境
热门文章
- 晶振两端的谐振电容有特殊要求吗_“吃瓜群众”也能秒懂的晶振电路原理
- 如何控制局域网网速_科普 | 路由器网速突然变慢怎么办?
- Feature Scaling(特征缩放)的一些方法和使用选择
- ~~朴素dijkstra算法 (搜索与图论)(附模板题AcWing 849. Dijkstra求最短路 I)
- algorithm头文件下的next_permutation()
- socket.io简介
- 更改电脑外部串口端口COM号
- 算法学习三:使用霍纳规则计算多项式
- linux/ubuntu中制作deb安装包
- 常用类 (一) ----- Arrays数组工具类