File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 35
35
| 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 ) |
36
36
| 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 ) |
37
37
| 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 ) |
39
39
| 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 ) |
40
40
| 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 ) |
41
41
| 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 ) |
Original file line number Diff line number Diff line change
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 ))
You can’t perform that action at this time.
0 commit comments