Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/my_project/interviews/top_150_questions_round_11/path_sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import List, Union, Collection, Mapping, Optional
from abc import ABC, abstractmethod

class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

class Solution:

def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:

if not root:
return False
elif not root.left and not root.right and root.val == targetSum:
return True
else:
temp_target = targetSum - root.val
return self.hasPathSum(root.left, temp_target) \
or self.hasPathSum(root.right, temp_target)
18 changes: 18 additions & 0 deletions tests/test_150_questions_round_11/test_path_sum_round_11.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import unittest
from src.my_project.interviews.top_150_questions_round_11\
.path_sum import TreeNode, Solution


class HasPathSumTestCase(unittest.TestCase):

def test_is_path_sum(self):
solution = Solution()
tree = TreeNode(1, TreeNode(2), TreeNode(3))
output = solution.hasPathSum(root=tree, targetSum=3)
self.assertTrue(output)

def test_is_no_path_sum(self):
solution = Solution()
tree = TreeNode(1, TreeNode(2), TreeNode(3))
output = solution.hasPathSum(root=tree, targetSum=10)
self.assertFalse(output)