-
Notifications
You must be signed in to change notification settings - Fork 0
/
JZ82.py
37 lines (33 loc) · 1.07 KB
/
JZ82.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
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param root TreeNode类
# @param sum int整型
# @return bool布尔型
#
class Solution:
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
def topDown(node: TreeNode, total: int) -> bool:
l = False
r = False
if node.left is None and node.right is None:
if total == sum:
return True
return False
elif node.left is None:
r = topDown(node.right, total + node.right.val)
elif node.right is None:
l = topDown(node.left, total + node.left.val)
else:
r = topDown(node.right, total + node.right.val)
l = topDown(node.left, total + node.left.val)
return l or r
if root is None:
return False
return topDown(root, root.val)