mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #335 from z80160280/master
添加 二叉树中递归带着回溯 198 337 python版本
This commit is contained in:
@ -131,7 +131,20 @@ class Solution {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def rob(self, nums: List[int]) -> int:
|
||||||
|
if len(nums) == 0:
|
||||||
|
return 0
|
||||||
|
if len(nums) == 1:
|
||||||
|
return nums[0]
|
||||||
|
dp = [0] * len(nums)
|
||||||
|
dp[0] = nums[0]
|
||||||
|
dp[1] = max(nums[0], nums[1])
|
||||||
|
for i in range(2, len(nums)):
|
||||||
|
dp[i] = max(dp[i-2]+nums[i], dp[i-1])
|
||||||
|
return dp[-1]
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
```Go
|
```Go
|
||||||
|
@ -287,6 +287,25 @@ class Solution {
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
> 动态规划
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def rob(self, root: TreeNode) -> int:
|
||||||
|
result = self.robTree(root)
|
||||||
|
return max(result[0], result[1])
|
||||||
|
|
||||||
|
#长度为2的数组,0:不偷,1:偷
|
||||||
|
def robTree(self, cur):
|
||||||
|
if not cur:
|
||||||
|
return (0, 0) #这里返回tuple, 也可以返回list
|
||||||
|
left = self.robTree(cur.left)
|
||||||
|
right = self.robTree(cur.right)
|
||||||
|
#偷cur
|
||||||
|
val1 = cur.val + left[0] + right[0]
|
||||||
|
#不偷cur
|
||||||
|
val2 = max(left[0], left[1]) + max(right[0], right[1])
|
||||||
|
return (val2, val1)
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
@ -257,6 +257,83 @@ Java:
|
|||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
100.相同的树
|
||||||
|
> 递归法
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
|
||||||
|
return self.compare(p, q)
|
||||||
|
|
||||||
|
def compare(self, tree1, tree2):
|
||||||
|
if not tree1 and tree2:
|
||||||
|
return False
|
||||||
|
elif tree1 and not tree2:
|
||||||
|
return False
|
||||||
|
elif not tree1 and not tree2:
|
||||||
|
return True
|
||||||
|
elif tree1.val != tree2.val: #注意这里我没有使用else
|
||||||
|
return False
|
||||||
|
|
||||||
|
#此时就是:左右节点都不为空,且数值相同的情况
|
||||||
|
#此时才做递归,做下一层的判断
|
||||||
|
compareLeft = self.compare(tree1.left, tree2.left) #左子树:左、 右子树:左
|
||||||
|
compareRight = self.compare(tree1.right, tree2.right) #左子树:右、 右子树:右
|
||||||
|
isSame = compareLeft and compareRight #左子树:中、 右子树:中(逻辑处理)
|
||||||
|
return isSame
|
||||||
|
```
|
||||||
|
|
||||||
|
257.二叉的所有路径
|
||||||
|
> 递归中隐藏着回溯
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def binaryTreePaths(self, root: TreeNode) -> List[str]:
|
||||||
|
result = []
|
||||||
|
path = []
|
||||||
|
if not root:
|
||||||
|
return result
|
||||||
|
self.traversal(root, path, result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def traversal(self, cur, path, result):
|
||||||
|
path.append(cur.val)
|
||||||
|
#这才到了叶子节点
|
||||||
|
if not cur.left and not cur.right:
|
||||||
|
sPath = ""
|
||||||
|
for i in range(len(path)-1):
|
||||||
|
sPath += str(path[i])
|
||||||
|
sPath += "->"
|
||||||
|
sPath += str(path[len(path)-1])
|
||||||
|
result.append(sPath)
|
||||||
|
return
|
||||||
|
if cur.left:
|
||||||
|
self.traversal(cur.left, path, result)
|
||||||
|
path.pop() #回溯
|
||||||
|
if cur.right:
|
||||||
|
self.traversal(cur.right, path, result)
|
||||||
|
path.pop() #回溯
|
||||||
|
```
|
||||||
|
|
||||||
|
> 精简版
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def binaryTreePaths(self, root: TreeNode) -> List[str]:
|
||||||
|
result = []
|
||||||
|
path = ""
|
||||||
|
if not root:
|
||||||
|
return result
|
||||||
|
self.traversal(root, path, result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def traversal(self, cur, path, result):
|
||||||
|
path += str(cur.val) #中
|
||||||
|
if not cur.left and not cur.right:
|
||||||
|
result.append(path)
|
||||||
|
return
|
||||||
|
if cur.left:
|
||||||
|
self.traversal(cur.left, path+"->", result) #左 回溯就隐藏在这里
|
||||||
|
if cur.right:
|
||||||
|
self.traversal(cur.right, path+"->", result) #右 回溯就隐藏在这里
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user