Skip to content

Commit 25e3010

Browse files
solves path sum in python
1 parent cf8f462 commit 25e3010

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
| 108 | [Convert Sorted Array To Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ConvertSortedArrayToBinarySearchTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/converted_sorted_array_to_binary_search_tree.py) |
3636
| 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/BalancedBinaryTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/balanced_binary_tree.py) |
3737
| 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/MinimumDepthOfBinaryTree.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/minimum_depth_of_binary_tree.py) |
38-
| 112 | [Path Sum](https://leetcode.com/problems/path-sum) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PathSum.java) |
38+
| 112 | [Path Sum](https://leetcode.com/problems/path-sum) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PathSum.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/path_sum.py) |
3939
| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PascalsTriangle.java) |
4040
| 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/PascalsTriangleII.java) |
4141
| 121 | [Best Time to Buy and Sell Stocks](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/BestTimeToBuyAndSellStock.java) |

python/path_sum.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
class TreeNode:
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
9+
class Solution:
10+
def _is_leaf_node(self, node: TreeNode) -> bool:
11+
return node.left is None and node.right is None
12+
13+
def _has_path_sum(self, root: TreeNode, target: int, currentSum: int) -> bool:
14+
if root is None:
15+
return False
16+
currentSum += root.val
17+
if self._is_leaf_node(root):
18+
return currentSum == target
19+
return self._has_path_sum(root.left, target, currentSum) or \
20+
self._has_path_sum(root.right, target, currentSum)
21+
22+
def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
23+
if root is None:
24+
return False
25+
return self._has_path_sum(root, targetSum, 0)
26+
27+
28+
root = TreeNode(val=1)
29+
root.right = TreeNode(val=2)
30+
print(Solution().hasPathSum(root, 3))

0 commit comments

Comments
 (0)