Skip to content

Commit a6a9b40

Browse files
committed
第三周的算法作业
1 parent 5b4d54d commit a6a9b40

8 files changed

+401
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# vscode
2+
.vscode/

Week_03/id_36/LeetCode_102_36.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#
2+
# @lc app=leetcode.cn id=102 lang=python3
3+
#
4+
# [102] 二叉树的层次遍历
5+
#
6+
# https://leetcode-cn.com/problems/binary-tree-level-order-traversal/description/
7+
#
8+
# algorithms
9+
# Medium (56.68%)
10+
# Likes: 215
11+
# Dislikes: 0
12+
# Total Accepted: 29.3K
13+
# Total Submissions: 51.7K
14+
# Testcase Example: '[3,9,20,null,null,15,7]'
15+
#
16+
# 给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
17+
#
18+
# 例如:
19+
# 给定二叉树: [3,9,20,null,null,15,7],
20+
#
21+
# ⁠ 3
22+
# ⁠ / \
23+
# ⁠ 9 20
24+
# ⁠ / \
25+
# ⁠ 15 7
26+
#
27+
#
28+
# 返回其层次遍历结果:
29+
#
30+
# [
31+
# ⁠ [3],
32+
# ⁠ [9,20],
33+
# ⁠ [15,7]
34+
# ]
35+
#
36+
#
37+
#
38+
# Definition for a binary tree node.
39+
class TreeNode:
40+
def __init__(self, x):
41+
self.val = x
42+
self.left = None
43+
self.right = None
44+
45+
# # 递归
46+
# class Solution:
47+
# def levelOrder(self, root: TreeNode) -> List[List[int]]:
48+
# levels = []
49+
# if not root:
50+
# return levels
51+
52+
# def helper(node, level):
53+
# if len(levels) == level:
54+
# levels.append([])
55+
56+
# levels[level].append(node.val)
57+
58+
# if node.left:
59+
# helper(node.left, level + 1)
60+
# if node.right:
61+
# helper(node.right, level + 1)
62+
63+
# helper(root, 0)
64+
# return levels
65+
66+
# BFS
67+
class Solution:
68+
def levelOrder(self, root: TreeNode) -> List[List[int]]:
69+
levels = []
70+
if not root:
71+
return levels
72+
73+
level = 0
74+
from collections import deque
75+
queue = deque([root,])
76+
77+
while queue:
78+
levels.append([])
79+
level_length = len(queue)
80+
81+
for i in range(level_length):
82+
node = queue.popleft()
83+
84+
levels[level].append(node.val)
85+
86+
if node.left:
87+
queue.append(node.left)
88+
if node.right:
89+
queue.append(node.right)
90+
91+
level += 1
92+
93+
return levels
94+

Week_03/id_36/LeetCode_104_36.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#
2+
# @lc app=leetcode.cn id=104 lang=python3
3+
#
4+
# [104] 二叉树的最大深度
5+
#
6+
# https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/
7+
#
8+
# algorithms
9+
# Easy (69.37%)
10+
# Likes: 298
11+
# Dislikes: 0
12+
# Total Accepted: 52.1K
13+
# Total Submissions: 75.1K
14+
# Testcase Example: '[3,9,20,null,null,15,7]'
15+
#
16+
# 给定一个二叉树,找出其最大深度。
17+
#
18+
# 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
19+
#
20+
# 说明: 叶子节点是指没有子节点的节点。
21+
#
22+
# 示例:
23+
# 给定二叉树 [3,9,20,null,null,15,7],
24+
#
25+
# ⁠ 3
26+
# ⁠ / \
27+
# ⁠ 9 20
28+
# ⁠ / \
29+
# ⁠ 15 7
30+
#
31+
# 返回它的最大深度 3 。
32+
#
33+
#
34+
# Definition for a binary tree node.
35+
36+
37+
class TreeNode:
38+
def __init__(self, x):
39+
self.val = x
40+
self.left = None
41+
self.right = None
42+
43+
# # 递归
44+
# class Solution:
45+
# def maxDepth(self, root: TreeNode) -> int:
46+
# if not root:
47+
# return 0
48+
# return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
49+
50+
# DFS
51+
class Solution:
52+
def maxDepth(self, root: TreeNode) -> int:
53+
if root is None:
54+
return 0
55+
56+
stack = []
57+
stack.append((root, 1))
58+
max_depth = 0
59+
while len(stack) > 0:
60+
current = stack.pop()
61+
root = current[0]
62+
current_depth = current[1]
63+
if root.left is None and root.right is None:
64+
max_depth = max(max_depth, current_depth)
65+
if root.left is not None:
66+
stack.append((root.left, current_depth + 1))
67+
if root.right is not None:
68+
stack.append((root.right, current_depth + 1))
69+
70+
return max_depth

Week_03/id_36/LeetCode_107_36.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#
2+
# @lc app=leetcode.cn id=107 lang=python3
3+
#
4+
# [107] 二叉树的层次遍历 II
5+
#
6+
# https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/description/
7+
#
8+
# algorithms
9+
# Easy (60.90%)
10+
# Likes: 108
11+
# Dislikes: 0
12+
# Total Accepted: 17.6K
13+
# Total Submissions: 28.9K
14+
# Testcase Example: '[3,9,20,null,null,15,7]'
15+
#
16+
# 给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)
17+
#
18+
# 例如:
19+
# 给定二叉树 [3,9,20,null,null,15,7],
20+
#
21+
# ⁠ 3
22+
# ⁠ / \
23+
# ⁠ 9 20
24+
# ⁠ / \
25+
# ⁠ 15 7
26+
#
27+
#
28+
# 返回其自底向上的层次遍历为:
29+
#
30+
# [
31+
# ⁠ [15,7],
32+
# ⁠ [9,20],
33+
# ⁠ [3]
34+
# ]
35+
#
36+
#
37+
#
38+
# Definition for a binary tree node.
39+
class TreeNode:
40+
def __init__(self, x):
41+
self.val = x
42+
self.left = None
43+
self.right = None
44+
45+
# BFS
46+
class Solution:
47+
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
48+
levels = []
49+
if not root:
50+
return levels
51+
52+
from collections import deque
53+
queue = deque([root,])
54+
children_list = []
55+
56+
while queue:
57+
level_length = len(queue)
58+
59+
for i in range(level_length):
60+
node = queue.popleft()
61+
62+
if node.left:
63+
queue.append(node.left)
64+
if node.right:
65+
queue.append(node.right)
66+
67+
children_list.append(node.val)
68+
69+
levels.insert(0, children_list)
70+
children_list = []
71+
72+
return levels
73+

Week_03/id_36/LeetCode_111_36.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#
2+
# @lc app=leetcode.cn id=111 lang=python3
3+
#
4+
# [111] 二叉树的最小深度
5+
#
6+
# Definition for a binary tree node.
7+
8+
9+
class TreeNode:
10+
def __init__(self, x):
11+
self.val = x
12+
self.left = None
13+
self.right = None
14+
15+
DFS
16+
class Solution:
17+
def minDepth(self, root: TreeNode) -> int:
18+
if root is None:
19+
return 0
20+
21+
stack = []
22+
import sys
23+
stack.append((root, 1))
24+
min_depth = sys.maxsize
25+
while len(stack) > 0:
26+
current = stack.pop()
27+
root = current[0]
28+
current_depth = current[1]
29+
if root.left is None and root.right is None:
30+
min_depth = min(min_depth, current_depth)
31+
if root.left is not None:
32+
stack.append((root.left, current_depth + 1))
33+
if root.right is not None:
34+
stack.append((root.right, current_depth + 1))
35+
36+
return min_depth

Week_03/id_36/LeetCode_200_36.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#
2+
# @lc app=leetcode.cn id=200 lang=python3
3+
#
4+
# [200] 岛屿数量
5+
#
6+
# https://leetcode-cn.com/problems/number-of-islands/description/
7+
#
8+
# algorithms
9+
# Medium (44.05%)
10+
# Likes: 171
11+
# Dislikes: 0
12+
# Total Accepted: 16.5K
13+
# Total Submissions: 37.4K
14+
# Testcase Example: '[["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]'
15+
#
16+
# 给定一个由 '1'(陆地)和
17+
# '0'(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
18+
#
19+
# 示例 1:
20+
#
21+
# 输入:
22+
# 11110
23+
# 11010
24+
# 11000
25+
# 00000
26+
#
27+
# 输出: 1
28+
#
29+
#
30+
# 示例 2:
31+
#
32+
# 输入:
33+
# 11000
34+
# 11000
35+
# 00100
36+
# 00011
37+
#
38+
# 输出: 3
39+
#
40+
#
41+
#
42+
43+
44+
class Solution:
45+
def numIslands(self, grid: List[List[str]]) -> int:
46+
def sink(i, j):
47+
if 0 <= i < len(grid) and 0 <= j < len(grid[0]) and grid[i][j] == '1':
48+
grid[i][j] = '0'
49+
list(map(sink, (i-1, i, i+1, i), (j, j-1, j, j+1)))
50+
return sum(grid[i][j] == '1' and not sink(i, j) for i in range(len(grid)) for j in range(len(grid[0])))

0 commit comments

Comments
 (0)