-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path112 Path Sum.py
More file actions
42 lines (36 loc) · 1.01 KB
/
112 Path Sum.py
File metadata and controls
42 lines (36 loc) · 1.01 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 10 23:12:06 2018
@author: yiqian
"""
# 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):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if not root:
return False
def visitNode(root, tempsum ,sumList):
if root.left or root.right:
if root.left:
visitNode(root.left, tempsum+root.val, sumList)
if root.right:
visitNode(root.right, tempsum+root.val, sumList)
else:
sumList.append(tempsum+root.val)
sumList = []
visitNode(root, 0, sumList)
print(sumList)
if sum in sumList:
return True
else:
return False