【leetcode❤python】 112. Path Sum
#-*- coding: UTF-8 -*-
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
sumList=[]
def dfs(self,root):
curSum=self.sumList[-1]
if root.left!=None or root.right!=None:self.sumList.pop()
else:return
if root.left!=None:
valsum=curSum+root.left.val
self.sumList.append(valsum)
self.dfs(root.left)
if root.right!=None:
valsum=curSum+root.right.val
self.sumList.append(valsum)
self.dfs(root.right)
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
self.sumList=[]
if root==None:return False
else:
self.sumList.append(root.val)
self.dfs(root)
if self.sumList.__contains__(sum):
return True
return False
【leetcode❤python】 112. Path Sum的更多相关文章
- 【LeetCode练习题】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 【leetcode❤python】 1. Two Sum
#-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object): def twoSum(self, nums, target): ...
- 【leetcode❤python】 303. Range Sum Query - Immutable
#-*- coding: UTF-8 -*- #Tags:dynamic programming,sumRange(i,j)=sum(j)-sum(i-1)class NumArray(object) ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 【一天一道LeetCode】#112. Path Sum
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【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 ...
- 【easy】112.path sum 113.-----------------
求是否有从根到叶的路径,节点和等于某个值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tree ...
- 【leetcode❤python】 Sum of Left Leaves
#-*- coding: UTF-8 -*- # Definition for a binary tree node.# class TreeNode(object):# def __init ...
- 【leetcode❤python】 58. Length of Last Word
#-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object): ...
随机推荐
- thinkphp 一个页面使用2次分页的方法
thinkphp内置ORG.Util.Page方法分页,使分页变得非常简单快捷. 但是如果一个页面里需要使用2次分页,就会产生冲突,这里先记录下百度来的解决办法 可以说是毫无技术含量的办法: 将Pag ...
- <c:if>条件判断 和 取值做乘法运算
http://www.yiibai.com/jstl/jstl_core_if_tag.html <td> ${log.etdhHandleTime }<span> < ...
- innodb double write buffer
两次写是innodb的一个重要特性,目的是为了保证在异常down机或者没电的情况下,保证数据的安全可靠.一次是往内存的double write buffer中写,一次是在刷共享表空间的连续128个页. ...
- laravel页面间的传值
可以在前端页面元素上添加onclick事件 onclick='selectRaw(this)' js中写function selectRaw(obj){ var data=$(obj).attr(& ...
- html5 canvas 笔记三(绘制文本和图片)
绘制文本 fillText(text, x, y [, maxWidth]) 在指定的(x,y)位置填充指定的文本,绘制的最大宽度是可选的. strokeText(text, x, y [, ma ...
- Linux下查看用户列表
cat /etc/passwd 可以查看所有用户的列表 w 可以查看当前活跃的用户列表 cat /etc/group 查看用户组
- linux下对sh文件的操作
1.创建test.sh文件 touch test.sh 2.编辑sh文件 vi test.sh(i:插入 | esc:退出insert模式 | wq+回车:退出) 3.保存退出 敲击esc, 然后输入 ...
- HDU 2222:Keywords Search(AC自动机模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2222 KMP是单模式串匹配的算法,而AC自动机是用于多模式串匹配的算法.主要由Trie和KMP的思想构成. 题意 ...
- Shell—学习之心得
由于项目要招聘需要有经验shell开发人员(awk编程),而作为技术面试官(暂时)的我对shell编程不太熟:当然以前也写过一些shell脚本来满足项目的需求—备份环境,数据库(逻辑).假如只是针对a ...
- MS Sq l数据类型
一.nchar : 适用于西文字符,一个字符占一个字节. 二.char: 可以中文,一个字符占两个字节. 三.var :可变. 四.varchar.nvarchar . 所以一般来说,如果含有中文字符 ...