Leetcode
[LeetCode] Path Sum 파이썬
YGSEO
2021. 4. 1. 02:19
728x90
class Solution:
# @param root, a tree node
# @param sum, an integer
# @return a boolean
def hasPathSum(self, root, sum):
if root is None:
return False
if root.left is None and root.right is None and root.val == sum:
return True
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
sum - curr_value 하면서 left, right node를 계속 탐색하다가 leaf 에 도착했을때 curr_value == sum-curr_value 이면 True
출처: github.com/jiapengwen/LeetCode/blob/master/Python/path-sum.py
728x90