mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
@ -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])]
|
||||||
|
@ -55,7 +55,7 @@
|
|||||||
|
|
||||||
参数必须有要遍历的树的根节点,还有就是一个int型的变量用来记录最长深度。 这里就不需要返回值了,所以递归函数的返回类型为void。
|
参数必须有要遍历的树的根节点,还有就是一个int型的变量用来记录最长深度。 这里就不需要返回值了,所以递归函数的返回类型为void。
|
||||||
|
|
||||||
本题还需要类里的两个全局变量,maxLen用来记录最大深度,result记录最大深度最左节点的数值。
|
本题还需要类里的两个全局变量,maxDepth用来记录最大深度,result记录最大深度最左节点的数值。
|
||||||
|
|
||||||
代码如下:
|
代码如下:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user