-
Notifications
You must be signed in to change notification settings - Fork 0
/
112_path-sum.py
55 lines (47 loc) · 1.41 KB
/
112_path-sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@author: rye
#@time: 2019/4/1
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
'''
自己做没做出来,虽然知道用递归,但是没想到sum - root.val,一直考虑用cnt去等于sum,所以没有很好的解决。
'''
class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if not root:
return False
elif root.left or root.right:
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
else:
return root.val == sum
class Solution1(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
def has(root, sum):
if not root.left and not root.right:
return sum == root.val
h = False
if root.left:
h = has(root.left, sum - root.val)
if not h and root.right:
h = has(root.right, sum - root.val)
return h
if not root:
return False
return has(root, sum)
if __name__ == '__main__':
pass