-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path112.path-sum.py
46 lines (35 loc) · 1.38 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
#
# @lc app=leetcode id=112 lang=python3
#
# [112] Path Sum
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isLeaf(self, node):
return not (node.left or node.right)
def pathSum(self, node, target, csum=0):
"""This is a recursive function auxillary helper method. It basically looks out for the computation.
An edge case I encountered was [1, 2] and the total of 1; Make sure that the node is leaf then only compare
Otherwise the comparsion is false.
Args:
node (TreeNode): some node
target (int): some path sum that should be reached
csum (int, optional): Keep track of current sum to help make decision about the path sum. Defaults to 0.
Returns:
bool: if a path from root to leaf can be added up to target
"""
if not node:
return False
cnodeval = node.val
if self.isLeaf(node):
return (csum + cnodeval) == target
return self.pathSum(node.left, target, csum + cnodeval) or self.pathSum(node.right, target, csum + cnodeval)
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
return self.pathSum(root, sum, 0)
# @lc code=end