[LeetCode] 112. Path Sum_Easy tag: DFS
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.
Example:
Given the below binary tree and sum = 22
,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path 5->4->11->2
which sum is 22.
典型的DFS, 然后需要将目前的path sum一起append进入stack里面, 然后判断path sum 是否跟给的sum相等并且是leaf, 即可.
1. Constraints
1) can be empty
2. IDeas
DFS: T:O(n) S: T(n)
3. Code
3.1) iterable
# iterable class Solution:
def pathSum(self, root, s):
if not root: return False
stack =[(root, root.val)]
while stack:
node, ps = stack.pop()
if ps == s and not node.left and not node.right:
return True
if node.left:
stack.append((node.left, ps + node.left.val))
if node.right:
stack.append((node.right, ps + node.right.val))
return False
3.2) recursive way.
class Solution:
def pathSum(self, root, s):
if not root: return False
if s- root.val == 0 and not root.left and not root.right:
return True
return self.pathSum(root.left, s- root.val) or self.pathSum(root.right, s- root.val)
4. Test cases
1) edge case
2) s= 9
5
/ \
4 8
[LeetCode] 112. Path Sum_Easy tag: DFS的更多相关文章
- [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...
- [LeetCode] 112. Path Sum 路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- (二叉树 DFS 递归) leetcode 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] 339. Nested List Weight Sum_Easy tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [LeetCode] 112. Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- Leetcode 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- LeetCode 112. Path Sum (二叉树路径之和)
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- LeetCode 112. Path Sum 二叉树的路径和 C++
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- 【Mac】WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
使用Mac 自带终端 链接服务器时候,报错如下 处理办法: 第一种: 直接删除: /users/username/.ssh/known_hosts 文件 第二种: ssh-keygen -R ...
- 【Linux】python 2.x 升级 python3.x 之后 yum命令出现except OSError, e: ^ SyntaxError: invalid syntax
python2.7升级到python3.6.4 文章链接 : https://zhuanlan.zhihu.com/p/33660059 我在服务器上.把linux默认安装的python2.7 升级 ...
- cxGrid使用汇总4
1. CxGrid汇总功能 ① OptionsView-Footer设置为True,显示页脚 ② CxGrid的Summary选项卡定义要汇总的列和字段名及汇总方式,Footer选项卡定义 ...
- 【Spring Boot&&Spring Cloud系列】Spring Boot中使用NoSql数据库Redis
github地址:https://github.com/AndyFlower/Spring-Boot-Learn/tree/master/spring-boot-nosql-redis 一.加入依赖到 ...
- UI设计中的高保真和低保真
低保真一般用Axure Rp产出,高保真分两种,带交互的或不带交互的.不带交互的高保真直接根据低保真用PS产出即可.带交互的,需要 PS产出后,再切图,再使用Axure RP与低保真结合产出高保真. ...
- LeetCode 29 Divide Two Integers (不使用乘法,除法,求模计算两个数的除法)
题目链接: https://leetcode.com/problems/divide-two-integers/?tab=Description Problem :不使用乘法,除法,求模计算两个数 ...
- sencha touch Model validations(模型验证,自定义验证)
model Ext.define('app.model.Register', { extend: 'Ext.data.Model', requires: ['Ext.data.JsonP'], con ...
- jenkins部署war包到远程服务器的tomcat
一.目的 jenkins上将war包,部署到远程服务器的tomcat上. 这边tomcat在windows 主机A上,版本apache-tomcat-8.5.23. jenkins在主机B上,cent ...
- 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验七:PS/2模块① — 键盘
实验七:PS/2模块① — 键盘 实验七依然也是熟烂的PS/2键盘.相较<建模篇>的PS/2键盘实验,实验七实除了实现基本的驱动以外,我们还要深入解PS/2时序,还有PS/2键盘的行为.不 ...
- IOS 7 更改导航栏文字到白色
To hide status bar in any viewcontroller: -(BOOL) prefersStatusBarHidden { return YES; } To change t ...