mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 13:00:22 +08:00
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]".
This commit is contained in:
@ -564,10 +564,10 @@ class Solution:
|
|||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
|
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
|
||||||
if root is None:
|
if root is None:
|
||||||
return False
|
return False
|
||||||
return self.traversal(root, sum - root.val)
|
return self.traversal(root, targetSum - root.val)
|
||||||
```
|
```
|
||||||
|
|
||||||
(版本二) 递归 + 精简
|
(版本二) 递归 + 精简
|
||||||
@ -579,12 +579,12 @@ class Solution:
|
|||||||
# self.left = left
|
# self.left = left
|
||||||
# self.right = right
|
# self.right = right
|
||||||
class Solution:
|
class Solution:
|
||||||
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
|
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
|
||||||
if not root:
|
if not root:
|
||||||
return False
|
return False
|
||||||
if not root.left and not root.right and sum == root.val:
|
if not root.left and not root.right and targetSum == root.val:
|
||||||
return True
|
return True
|
||||||
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
|
return self.hasPathSum(root.left, targetSum - root.val) or self.hasPathSum(root.right, targetSum - root.val)
|
||||||
|
|
||||||
```
|
```
|
||||||
(版本三) 迭代
|
(版本三) 迭代
|
||||||
@ -596,7 +596,7 @@ class Solution:
|
|||||||
# self.left = left
|
# self.left = left
|
||||||
# self.right = right
|
# self.right = right
|
||||||
class Solution:
|
class Solution:
|
||||||
def hasPathSum(self, root: TreeNode, sum: int) -> bool:
|
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
|
||||||
if not root:
|
if not root:
|
||||||
return False
|
return False
|
||||||
# 此时栈里要放的是pair<节点指针,路径数值>
|
# 此时栈里要放的是pair<节点指针,路径数值>
|
||||||
@ -659,13 +659,13 @@ class Solution:
|
|||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
|
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
|
||||||
self.result.clear()
|
self.result.clear()
|
||||||
self.path.clear()
|
self.path.clear()
|
||||||
if not root:
|
if not root:
|
||||||
return self.result
|
return self.result
|
||||||
self.path.append(root.val) # 把根节点放进路径
|
self.path.append(root.val) # 把根节点放进路径
|
||||||
self.traversal(root, sum - root.val)
|
self.traversal(root, targetSum - root.val)
|
||||||
return self.result
|
return self.result
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -678,7 +678,7 @@ class Solution:
|
|||||||
# self.left = left
|
# self.left = left
|
||||||
# self.right = right
|
# self.right = right
|
||||||
class Solution:
|
class Solution:
|
||||||
def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
|
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
|
||||||
|
|
||||||
result = []
|
result = []
|
||||||
self.traversal(root, targetSum, [], result)
|
self.traversal(root, targetSum, [], result)
|
||||||
@ -703,7 +703,7 @@ class Solution:
|
|||||||
# self.left = left
|
# self.left = left
|
||||||
# self.right = right
|
# self.right = right
|
||||||
class Solution:
|
class Solution:
|
||||||
def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
|
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
|
||||||
if not root:
|
if not root:
|
||||||
return []
|
return []
|
||||||
stack = [(root, [root.val])]
|
stack = [(root, [root.val])]
|
||||||
|
Reference in New Issue
Block a user