mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
更改 0112.路径总和.md Python3 代码.
0112.路径总和: 1. 修改了 python3 注释, 更改代码格式. 2. 增加了迭代方法Python3代码. 0113.路径总和-ii: 上一版python3代码的问题: (1)注释一般不用// (2)代码格式有些不规范. (3)内层函数命名也不是特别的informative. 新版本代码修改了上述问题. 本次提交代码均已通过leetcode测试.
This commit is contained in:
@ -416,6 +416,8 @@ class Solution {
|
||||
Python:
|
||||
|
||||
0112.路径总和
|
||||
|
||||
**递归**
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
@ -424,28 +426,56 @@ Python:
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
|
||||
// 递归法
|
||||
|
||||
class Solution:
|
||||
def hasPathSum(self, root: TreeNode, targetSum: int) -> 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):return False //遇到叶子节点,计数不为0
|
||||
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):
|
||||
return False # 遇到叶子节点,计数不为0
|
||||
if root.left:
|
||||
targetSum -= root.left.val //左节点
|
||||
if isornot(root.left,targetSum):return True //递归,处理左节点
|
||||
targetSum += root.left.val //回溯
|
||||
targetSum -= root.left.val # 左节点
|
||||
if isornot(root.left, targetSum): return True # 递归,处理左节点
|
||||
targetSum += root.left.val # 回溯
|
||||
if root.right:
|
||||
targetSum -= root.right.val //右节点
|
||||
if isornot(root.right,targetSum):return True //递归,处理右节点
|
||||
targetSum += root.right.val //回溯
|
||||
targetSum -= root.right.val # 右节点
|
||||
if isornot(root.right, targetSum): return True # 递归,处理右节点
|
||||
targetSum += root.right.val # 回溯
|
||||
return False
|
||||
|
||||
if root == None:return False //别忘记处理空TreeNode
|
||||
else:return isornot(root,targetSum-root.val)
|
||||
|
||||
if root == None:
|
||||
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
|
||||
|
||||
**递归**
|
||||
```python
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
@ -453,35 +483,36 @@ class Solution:
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
//递归法
|
||||
class Solution:
|
||||
def pathSum(self, root: TreeNode, targetSum: int) -> List[List[int]]:
|
||||
path=[]
|
||||
res=[]
|
||||
def pathes(root,targetSum):
|
||||
if (not root.left) and (not root.right) and targetSum == 0: // 遇到叶子节点,并且计数为0
|
||||
res.append(path[:]) //找到一种路径,记录到res中,注意必须是path[:]而不是path
|
||||
return
|
||||
if (not root.left) and (not root.right):return // 遇到叶子节点直接返回
|
||||
if root.left: //左
|
||||
targetSum -= root.left.val
|
||||
path.append(root.left.val) //递归前记录节点
|
||||
pathes(root.left,targetSum) //递归
|
||||
targetSum += root.left.val //回溯
|
||||
path.pop() //回溯
|
||||
if root.right: //右
|
||||
targetSum -= root.right.val
|
||||
path.append(root.right.val) //递归前记录节点
|
||||
pathes(root.right,targetSum) //递归
|
||||
targetSum += root.right.val //回溯
|
||||
path.pop() //回溯
|
||||
return
|
||||
|
||||
if root == None:return [] //处理空TreeNode
|
||||
else:
|
||||
path.append(root.val) //首先处理根节点
|
||||
pathes(root,targetSum-root.val)
|
||||
return res
|
||||
|
||||
def traversal(cur_node, remain):
|
||||
if not cur_node.left and not cur_node.right and remain == 0:
|
||||
result.append(path[:])
|
||||
return
|
||||
|
||||
if not cur_node.left and not cur_node.right: return
|
||||
|
||||
if cur_node.left:
|
||||
path.append(cur_node.left.val)
|
||||
remain -= cur_node.left.val
|
||||
traversal(cur_node.left, remain)
|
||||
path.pop()
|
||||
remain += cur_node.left.val
|
||||
|
||||
if cur_node.right:
|
||||
path.append(cur_node.right.val)
|
||||
remain -= cur_node.right.val
|
||||
traversal(cur_node.right, remain)
|
||||
path.pop()
|
||||
remain += cur_node.right.val
|
||||
|
||||
result, path = [], []
|
||||
if not root:
|
||||
return []
|
||||
path.append(root.val)
|
||||
traversal(root, targetSum - root.val)
|
||||
return result
|
||||
```
|
||||
|
||||
Go:
|
||||
|
Reference in New Issue
Block a user