1. 문제
2. 나의 풀이
class Solution(object):
def hasChild(self, node):
if node and node.left:
return True
if node and node.right:
return True
return False
def isTargetSumExist(self, node, prevSum, target):
if not node:
return False
newSum = prevSum + node.val
if not self.hasChild(node):
if newSum == target:
return True
else:
return False
if(self.isTargetSumExist(node.left, newSum, target)):
return True
if(self.isTargetSumExist(node.right, newSum, target)):
return True
return False
def hasPathSum(self, root, targetSum):
"""
:type root: TreeNode
:type targetSum: int
:rtype: bool
"""
return self.isTargetSumExist(root, 0, targetSum)
3. 남의 풀이