mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Merge branch 'master' into patch-6
This commit is contained in:
@ -348,6 +348,7 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
0112.路径总和
|
0112.路径总和
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
@ -356,7 +357,9 @@ Python:
|
|||||||
# self.val = val
|
# self.val = val
|
||||||
# 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:
|
||||||
@ -375,6 +378,7 @@ class Solution:
|
|||||||
if root == None:return False //别忘记处理空TreeNode
|
if root == None:return False //别忘记处理空TreeNode
|
||||||
else:return isornot(root,targetSum-root.val)
|
else:return isornot(root,targetSum-root.val)
|
||||||
```
|
```
|
||||||
|
|
||||||
0113.路径总和-ii
|
0113.路径总和-ii
|
||||||
```python
|
```python
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
@ -413,6 +417,7 @@ class Solution:
|
|||||||
pathes(root,targetSum-root.val)
|
pathes(root,targetSum-root.val)
|
||||||
return res
|
return res
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
JavaScript:
|
JavaScript:
|
||||||
|
@ -160,7 +160,28 @@ Python:
|
|||||||
|
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
```Go
|
||||||
|
func findLength(A []int, B []int) int {
|
||||||
|
m, n := len(A), len(B)
|
||||||
|
res := 0
|
||||||
|
dp := make([][]int, m+1)
|
||||||
|
for i := 0; i <= m; i++ {
|
||||||
|
dp[i] = make([]int, n+1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 1; i <= m; i++ {
|
||||||
|
for j := 1; j <= n; j++ {
|
||||||
|
if A[i-1] == B[j-1] {
|
||||||
|
dp[i][j] = dp[i-1][j-1] + 1
|
||||||
|
}
|
||||||
|
if dp[i][j] > res {
|
||||||
|
res = dp[i][j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -170,7 +170,53 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python3
|
||||||
|
# 前序遍历-递归-LC144_二叉树的前序遍历
|
||||||
|
class Solution:
|
||||||
|
def preorderTraversal(self, root: TreeNode) -> List[int]:
|
||||||
|
# 保存结果
|
||||||
|
result = []
|
||||||
|
|
||||||
|
def traversal(root: TreeNode):
|
||||||
|
if root == None:
|
||||||
|
return
|
||||||
|
result.append(root.val) # 前序
|
||||||
|
traversal(root.left) # 左
|
||||||
|
traversal(root.right) # 右
|
||||||
|
|
||||||
|
traversal(root)
|
||||||
|
return result
|
||||||
|
|
||||||
|
# 中序遍历-递归-LC94_二叉树的中序遍历
|
||||||
|
class Solution:
|
||||||
|
def inorderTraversal(self, root: TreeNode) -> List[int]:
|
||||||
|
result = []
|
||||||
|
|
||||||
|
def traversal(root: TreeNode):
|
||||||
|
if root == None:
|
||||||
|
return
|
||||||
|
traversal(root.left) # 左
|
||||||
|
result.append(root.val) # 中序
|
||||||
|
traversal(root.right) # 右
|
||||||
|
|
||||||
|
traversal(root)
|
||||||
|
return result
|
||||||
|
|
||||||
|
# 后序遍历-递归-LC145_二叉树的后序遍历
|
||||||
|
class Solution:
|
||||||
|
def postorderTraversal(self, root: TreeNode) -> List[int]:
|
||||||
|
result = []
|
||||||
|
|
||||||
|
def traversal(root: TreeNode):
|
||||||
|
if root == None:
|
||||||
|
return
|
||||||
|
traversal(root.left) # 左
|
||||||
|
traversal(root.right) # 右
|
||||||
|
result.append(root.val) # 后序
|
||||||
|
|
||||||
|
traversal(root)
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user