mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge pull request #536 from KelvinG-611/112.路径总和
更改 0112.路径总和.md Python3 代码.
This commit is contained in:
@ -416,6 +416,8 @@ class Solution {
|
|||||||
Python:
|
Python:
|
||||||
|
|
||||||
0112.路径总和
|
0112.路径总和
|
||||||
|
|
||||||
|
**递归**
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode:
|
# class TreeNode:
|
||||||
@ -424,28 +426,56 @@ Python:
|
|||||||
# self.left = left
|
# self.left = left
|
||||||
# self.right = right
|
# self.right = right
|
||||||
|
|
||||||
// 递归法
|
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
|
def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
|
||||||
def isornot(root,targetSum)->bool:
|
def isornot(root, targetSum) -> bool:
|
||||||
if (not root.left) and (not root.right) and targetSum == 0:return True // 遇到叶子节点,并且计数为0
|
if (not root.left) and (not root.right) and targetSum == 0:
|
||||||
if (not root.left) and (not root.right):return False //遇到叶子节点,计数不为0
|
return True # 遇到叶子节点,并且计数为0
|
||||||
|
if (not root.left) and (not root.right):
|
||||||
|
return False # 遇到叶子节点,计数不为0
|
||||||
if root.left:
|
if root.left:
|
||||||
targetSum -= root.left.val //左节点
|
targetSum -= root.left.val # 左节点
|
||||||
if isornot(root.left,targetSum):return True //递归,处理左节点
|
if isornot(root.left, targetSum): return True # 递归,处理左节点
|
||||||
targetSum += root.left.val //回溯
|
targetSum += root.left.val # 回溯
|
||||||
if root.right:
|
if root.right:
|
||||||
targetSum -= root.right.val //右节点
|
targetSum -= root.right.val # 右节点
|
||||||
if isornot(root.right,targetSum):return True //递归,处理右节点
|
if isornot(root.right, targetSum): return True # 递归,处理右节点
|
||||||
targetSum += root.right.val //回溯
|
targetSum += root.right.val # 回溯
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if root == None:return False //别忘记处理空TreeNode
|
if root == None:
|
||||||
else:return isornot(root,targetSum-root.val)
|
return False # 别忘记处理空TreeNode
|
||||||
|
else:
|
||||||
|
return isornot(root, targetSum - root.val)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**迭代 - 层序遍历**
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
|
||||||
|
if not root:
|
||||||
|
return False
|
||||||
|
|
||||||
|
stack = [] # [(当前节点,路径数值), ...]
|
||||||
|
stack.append((root, root.val))
|
||||||
|
|
||||||
|
while stack:
|
||||||
|
cur_node, path_sum = stack.pop()
|
||||||
|
|
||||||
|
if not cur_node.left and not cur_node.right and path_sum == targetSum:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if cur_node.right:
|
||||||
|
stack.append((cur_node.right, path_sum + cur_node.right.val))
|
||||||
|
|
||||||
|
if cur_node.left:
|
||||||
|
stack.append((cur_node.left, path_sum + cur_node.left.val))
|
||||||
|
|
||||||
|
return False
|
||||||
|
```
|
||||||
0113.路径总和-ii
|
0113.路径总和-ii
|
||||||
|
|
||||||
|
**递归**
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode:
|
# class TreeNode:
|
||||||
@ -453,35 +483,36 @@ class Solution:
|
|||||||
# self.val = val
|
# self.val = val
|
||||||
# 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: TreeNode, targetSum: int) -> List[List[int]]:
|
||||||
path=[]
|
|
||||||
res=[]
|
def traversal(cur_node, remain):
|
||||||
def pathes(root,targetSum):
|
if not cur_node.left and not cur_node.right and remain == 0:
|
||||||
if (not root.left) and (not root.right) and targetSum == 0: // 遇到叶子节点,并且计数为0
|
result.append(path[:])
|
||||||
res.append(path[:]) //找到一种路径,记录到res中,注意必须是path[:]而不是path
|
return
|
||||||
return
|
|
||||||
if (not root.left) and (not root.right):return // 遇到叶子节点直接返回
|
if not cur_node.left and not cur_node.right: return
|
||||||
if root.left: //左
|
|
||||||
targetSum -= root.left.val
|
if cur_node.left:
|
||||||
path.append(root.left.val) //递归前记录节点
|
path.append(cur_node.left.val)
|
||||||
pathes(root.left,targetSum) //递归
|
remain -= cur_node.left.val
|
||||||
targetSum += root.left.val //回溯
|
traversal(cur_node.left, remain)
|
||||||
path.pop() //回溯
|
path.pop()
|
||||||
if root.right: //右
|
remain += cur_node.left.val
|
||||||
targetSum -= root.right.val
|
|
||||||
path.append(root.right.val) //递归前记录节点
|
if cur_node.right:
|
||||||
pathes(root.right,targetSum) //递归
|
path.append(cur_node.right.val)
|
||||||
targetSum += root.right.val //回溯
|
remain -= cur_node.right.val
|
||||||
path.pop() //回溯
|
traversal(cur_node.right, remain)
|
||||||
return
|
path.pop()
|
||||||
|
remain += cur_node.right.val
|
||||||
if root == None:return [] //处理空TreeNode
|
|
||||||
else:
|
result, path = [], []
|
||||||
path.append(root.val) //首先处理根节点
|
if not root:
|
||||||
pathes(root,targetSum-root.val)
|
return []
|
||||||
return res
|
path.append(root.val)
|
||||||
|
traversal(root, targetSum - root.val)
|
||||||
|
return result
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
Reference in New Issue
Block a user