Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d0f6653

Browse files
Update 0112.路径总和.md
it's important to avoid using reserved keywords as variable names. so i suggest changing "sum" to "targetSum" In order to maintain consistency with the original LeetCode problem, the term "TreeNode" can be replaced with "Optional[TreeNode]".
1 parent 4c37ced commit d0f6653

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

‎problems/0112.路径总和.md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -564,10 +564,10 @@ class Solution:
564564

565565
return False
566566

567-
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
567+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
568568
if root is None:
569569
return False
570-
return self.traversal(root, sum - root.val)
570+
return self.traversal(root, targetSum - root.val)
571571
```
572572

573573
(版本二) 递归 + 精简
@@ -579,12 +579,12 @@ class Solution:
579579
# self.left = left
580580
# self.right = right
581581
class Solution:
582-
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
582+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
583583
if not root:
584584
return False
585-
if not root.left and not root.right and sum == root.val:
585+
if not root.left and not root.right and targetSum == root.val:
586586
return True
587-
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
587+
return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(root.right, targetSum - root.val)
588588

589589
```
590590
(版本三) 迭代
@@ -596,7 +596,7 @@ class Solution:
596596
# self.left = left
597597
# self.right = right
598598
class Solution:
599-
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
599+
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
600600
if not root:
601601
return False
602602
# 此时栈里要放的是pair<节点指针,路径数值>
@@ -659,13 +659,13 @@ class Solution:
659659

660660
return
661661

662-
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
662+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
663663
self.result.clear()
664664
self.path.clear()
665665
if not root:
666666
return self.result
667667
self.path.append(root.val) # 把根节点放进路径
668-
self.traversal(root, sum - root.val)
668+
self.traversal(root, targetSum - root.val)
669669
return self.result
670670
```
671671

@@ -678,7 +678,7 @@ class Solution:
678678
# self.left = left
679679
# self.right = right
680680
class Solution:
681-
def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
681+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
682682

683683
result = []
684684
self.traversal(root, targetSum, [], result)
@@ -703,7 +703,7 @@ class Solution:
703703
# self.left = left
704704
# self.right = right
705705
class Solution:
706-
def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
706+
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
707707
if not root:
708708
return []
709709
stack = [(root, [root.val])]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /