mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
Merge branch 'youngyangyang04:master' into master
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
|
|
||||||
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
|
return
|
||||||
|
|
||||||
if root == None:return [] //处理空TreeNode
|
if not cur_node.left and not cur_node.right: return
|
||||||
else:
|
|
||||||
path.append(root.val) //首先处理根节点
|
if cur_node.left:
|
||||||
pathes(root,targetSum-root.val)
|
path.append(cur_node.left.val)
|
||||||
return res
|
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:
|
Go:
|
||||||
|
@ -205,25 +205,51 @@ class Solution {
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
```Python
|
|
||||||
**递归**
|
**递归**
|
||||||
# Definition for a binary tree node.
|
```python
|
||||||
# class TreeNode:
|
|
||||||
# def __init__(self, val=0, left=None, right=None):
|
|
||||||
# self.val = val
|
|
||||||
# self.left = left
|
|
||||||
# self.right = right
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def sumOfLeftLeaves(self, root: TreeNode) -> int:
|
def sumOfLeftLeaves(self, root: TreeNode) -> int:
|
||||||
self.res=0
|
if not root:
|
||||||
def areleftleaves(root):
|
return 0
|
||||||
if not root:return
|
|
||||||
if root.left and (not root.left.left) and (not root.left.right):self.res+=root.left.val
|
left_left_leaves_sum = self.sumOfLeftLeaves(root.left) # 左
|
||||||
areleftleaves(root.left)
|
right_left_leaves_sum = self.sumOfLeftLeaves(root.right) # 右
|
||||||
areleftleaves(root.right)
|
|
||||||
areleftleaves(root)
|
cur_left_leaf_val = 0
|
||||||
return self.res
|
if root.left and not root.left.left and not root.left.right:
|
||||||
|
cur_left_leaf_val = root.left.val # 中
|
||||||
|
|
||||||
|
return cur_left_leaf_val + left_left_leaves_sum + right_left_leaves_sum
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**迭代**
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def sumOfLeftLeaves(self, root: TreeNode) -> int:
|
||||||
|
"""
|
||||||
|
Idea: Each time check current node's left node.
|
||||||
|
If current node don't have one, skip it.
|
||||||
|
"""
|
||||||
|
stack = []
|
||||||
|
if root:
|
||||||
|
stack.append(root)
|
||||||
|
res = 0
|
||||||
|
|
||||||
|
while stack:
|
||||||
|
# 每次都把当前节点的左节点加进去.
|
||||||
|
cur_node = stack.pop()
|
||||||
|
if cur_node.left and not cur_node.left.left and not cur_node.left.right:
|
||||||
|
res += cur_node.left.val
|
||||||
|
|
||||||
|
if cur_node.left:
|
||||||
|
stack.append(cur_node.left)
|
||||||
|
if cur_node.right:
|
||||||
|
stack.append(cur_node.right)
|
||||||
|
|
||||||
|
return res
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
> 递归法
|
> 递归法
|
||||||
|
@ -274,27 +274,51 @@ class Solution {
|
|||||||
|
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
**递归 - 回溯**
|
||||||
```python
|
```python
|
||||||
//递归法
|
|
||||||
# Definition for a binary tree node.
|
|
||||||
# class TreeNode:
|
|
||||||
# def __init__(self, val=0, left=None, right=None):
|
|
||||||
# self.val = val
|
|
||||||
# self.left = left
|
|
||||||
# self.right = right
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def findBottomLeftValue(self, root: TreeNode) -> int:
|
def findBottomLeftValue(self, root: TreeNode) -> int:
|
||||||
depth=0
|
max_depth = -float("INF")
|
||||||
self.res=[]
|
leftmost_val = 0
|
||||||
def level(root,depth):
|
|
||||||
if not root:return
|
def __traverse(root, cur_depth):
|
||||||
if depth==len(self.res):
|
nonlocal max_depth, leftmost_val
|
||||||
self.res.append([])
|
if not root.left and not root.right:
|
||||||
self.res[depth].append(root.val)
|
if cur_depth > max_depth:
|
||||||
level(root.left,depth+1)
|
max_depth = cur_depth
|
||||||
level(root.right,depth+1)
|
leftmost_val = root.val
|
||||||
level(root,depth)
|
if root.left:
|
||||||
return self.res[-1][0]
|
cur_depth += 1
|
||||||
|
__traverse(root.left, cur_depth)
|
||||||
|
cur_depth -= 1
|
||||||
|
if root.right:
|
||||||
|
cur_depth += 1
|
||||||
|
__traverse(root.right, cur_depth)
|
||||||
|
cur_depth -= 1
|
||||||
|
|
||||||
|
__traverse(root, 0)
|
||||||
|
return leftmost_val
|
||||||
|
```
|
||||||
|
**迭代 - 层序遍历**
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def findBottomLeftValue(self, root: TreeNode) -> int:
|
||||||
|
queue = deque()
|
||||||
|
if root:
|
||||||
|
queue.append(root)
|
||||||
|
result = 0
|
||||||
|
while queue:
|
||||||
|
q_len = len(queue)
|
||||||
|
for i in range(q_len):
|
||||||
|
if i == 0:
|
||||||
|
result = queue[i].val
|
||||||
|
cur = queue.popleft()
|
||||||
|
if cur.left:
|
||||||
|
queue.append(cur.left)
|
||||||
|
if cur.right:
|
||||||
|
queue.append(cur.right)
|
||||||
|
return result
|
||||||
```
|
```
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
@ -252,6 +252,33 @@ func findLength(A []int, B []int) int {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
JavaScript:
|
||||||
|
|
||||||
|
> 动态规划
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const findLength = (A, B) => {
|
||||||
|
// A、B数组的长度
|
||||||
|
const [m, n] = [A.length, B.length];
|
||||||
|
// dp数组初始化,都初始化为0
|
||||||
|
const dp = new Array(m + 1).fill(0).map(x => new Array(n + 1).fill(0));
|
||||||
|
// 初始化最大长度为0
|
||||||
|
let res = 0;
|
||||||
|
for (let i = 1; i <= m; i++) {
|
||||||
|
for (let j = 1; j <= n; j++) {
|
||||||
|
// 遇到A[i - 1] === B[j - 1],则更新dp数组
|
||||||
|
if (A[i - 1] === B[j - 1]) {
|
||||||
|
dp[i][j] = dp[i - 1][j - 1] + 1;
|
||||||
|
}
|
||||||
|
// 更新res
|
||||||
|
res = dp[i][j] > res ? dp[i][j] : res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 遍历完成,返回res
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user