Skip to content

Commit bf4f9cd

Browse files
committed
feat: update solutions to lc problems: No.0111,0199
* No.0111.Minimum Depth of Binary Tree * No.0199.Binary Tree Right Side View
1 parent f86e2df commit bf4f9cd

File tree

21 files changed

+267
-157
lines changed

21 files changed

+267
-157
lines changed

solution/0100-0199/0104.Maximum Depth of Binary Tree/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ func max(a, b int) int {
149149
* @return {number}
150150
*/
151151
var maxDepth = function (root) {
152-
if (!root) {
153-
return 0;
154-
}
152+
if (!root) return 0;
155153
const l = maxDepth(root.left);
156154
const r = maxDepth(root.right);
157155
return 1 + Math.max(l, r);

solution/0100-0199/0104.Maximum Depth of Binary Tree/README_EN.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,7 @@ func max(a, b int) int {
179179
* @return {number}
180180
*/
181181
var maxDepth = function (root) {
182-
if (!root) {
183-
return 0;
184-
}
182+
if (!root) return 0;
185183
const l = maxDepth(root.left);
186184
const r = maxDepth(root.right);
187185
return 1 + Math.max(l, r);

solution/0100-0199/0104.Maximum Depth of Binary Tree/Solution.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
* @return {number}
1212
*/
1313
var maxDepth = function (root) {
14-
if (!root) {
15-
return 0;
16-
}
14+
if (!root) return 0;
1715
const l = maxDepth(root.left);
1816
const r = maxDepth(root.right);
1917
return 1 + Math.max(l, r);

solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class Solution {
9898
q.offerLast(root);
9999
while (!q.isEmpty()) {
100100
List<Integer> t = new ArrayList<>();
101-
for (int i = 0, n = q.size(); i < n; ++i) {
101+
for (int i = q.size(); i > 0; --i) {
102102
TreeNode node = q.pollFirst();
103103
t.add(node.val);
104104
if (node.left != null) {
@@ -138,8 +138,7 @@ public:
138138
while (!q.empty())
139139
{
140140
vector<int> t;
141-
for (int i = 0, n = q.size(); i < n; ++i)
142-
{
141+
for (int i = q.size(); i > 0; --i) {
143142
TreeNode* node = q.front();
144143
q.pop();
145144
t.push_back(node->val);
@@ -173,7 +172,7 @@ func levelOrderBottom(root *TreeNode) [][]int {
173172
q := []*TreeNode{root}
174173
for len(q) > 0 {
175174
var t []int
176-
for i, n := 0, len(q); i < n; i++ {
175+
for i := len(q); i > 0; i-- {
177176
node := q[0]
178177
q = q[1:]
179178
t = append(t, node.Val)
@@ -210,9 +209,8 @@ var levelOrderBottom = function (root) {
210209
if (!root) return ans;
211210
let q = [root];
212211
while (q.length) {
213-
let n = q.length;
214212
let t = [];
215-
while (n-- > 0) {
213+
for (let i = q.length; i > 0; --i) {
216214
const node = q.shift();
217215
t.push(node.val);
218216
if (node.left) q.push(node.left);

solution/0100-0199/0107.Binary Tree Level Order Traversal II/README_EN.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class Solution {
9797
q.offerLast(root);
9898
while (!q.isEmpty()) {
9999
List<Integer> t = new ArrayList<>();
100-
for (int i = 0, n = q.size(); i < n; ++i) {
100+
for (int i = q.size(); i > 0; --i) {
101101
TreeNode node = q.pollFirst();
102102
t.add(node.val);
103103
if (node.left != null) {
@@ -137,8 +137,7 @@ public:
137137
while (!q.empty())
138138
{
139139
vector<int> t;
140-
for (int i = 0, n = q.size(); i < n; ++i)
141-
{
140+
for (int i = q.size(); i > 0; --i) {
142141
TreeNode* node = q.front();
143142
q.pop();
144143
t.push_back(node->val);
@@ -172,7 +171,7 @@ func levelOrderBottom(root *TreeNode) [][]int {
172171
q := []*TreeNode{root}
173172
for len(q) > 0 {
174173
var t []int
175-
for i, n := 0, len(q); i < n; i++ {
174+
for i := len(q); i > 0; i-- {
176175
node := q[0]
177176
q = q[1:]
178177
t = append(t, node.Val)
@@ -209,9 +208,8 @@ var levelOrderBottom = function (root) {
209208
if (!root) return ans;
210209
let q = [root];
211210
while (q.length) {
212-
let n = q.length;
213211
let t = [];
214-
while (n-- > 0) {
212+
for (let i = q.length; i > 0; --i) {
215213
const node = q.shift();
216214
t.push(node.val);
217215
if (node.left) q.push(node.left);

solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class Solution {
1818
while (!q.empty())
1919
{
2020
vector<int> t;
21-
for (int i = 0, n = q.size(); i < n; ++i)
22-
{
21+
for (int i = q.size(); i > 0; --i) {
2322
TreeNode* node = q.front();
2423
q.pop();
2524
t.push_back(node->val);

solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func levelOrderBottom(root *TreeNode) [][]int {
1414
q := []*TreeNode{root}
1515
for len(q) > 0 {
1616
var t []int
17-
for i, n := 0, len(q); i < n; i++ {
17+
for i := len(q); i > 0; i-- {
1818
node := q[0]
1919
q = q[1:]
2020
t = append(t, node.Val)

solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public List<List<Integer>> levelOrderBottom(TreeNode root) {
2323
q.offerLast(root);
2424
while (!q.isEmpty()) {
2525
List<Integer> t = new ArrayList<>();
26-
for (int i = 0, n = q.size(); i < n; ++i) {
26+
for (int i = q.size(); i > 0; --i) {
2727
TreeNode node = q.pollFirst();
2828
t.add(node.val);
2929
if (node.left != null) {

solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ var levelOrderBottom = function (root) {
1515
if (!root) return ans;
1616
let q = [root];
1717
while (q.length) {
18-
let n = q.length;
1918
let t = [];
20-
while (n-- > 0) {
19+
for (let i = q.length; i > 0; --i) {
2120
const node = q.shift();
2221
t.push(node.val);
2322
if (node.left) q.push(node.left);

solution/0100-0199/0111.Minimum Depth of Binary Tree/README.md

+70-26
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
若左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度加 1;左右子树都不为空,返回最小深度加 1 即可。
45+
4446
<!-- tabs:start -->
4547

4648
### **Python3**
@@ -56,15 +58,16 @@
5658
# self.right = right
5759
class Solution:
5860
def minDepth(self, root: TreeNode) -> int:
59-
if root is None:
60-
return 0
61-
# 如果左子树和右子树其中一个为空,那么需要返回比较大的那个子树的深度+1
62-
if root.left is None:
63-
return 1 + self.minDepth(root.right)
64-
if root.right is None:
65-
return 1 + self.minDepth(root.left)
66-
# 左右子树都不为空,返回最小深度+1即可
67-
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
61+
def dfs(root):
62+
if root is None:
63+
return 0
64+
if root.left is None:
65+
return 1 + dfs(root.right)
66+
if root.right is None:
67+
return 1 + dfs(root.left)
68+
return 1 + min(dfs(root.left), dfs(root.right))
69+
70+
return dfs(root)
6871
```
6972

7073
### **Java**
@@ -89,16 +92,20 @@ class Solution:
8992
*/
9093
class Solution {
9194
public int minDepth(TreeNode root) {
95+
return dfs(root);
96+
}
97+
98+
private int dfs(TreeNode root) {
9299
if (root == null) {
93100
return 0;
94101
}
95102
if (root.left == null) {
96-
return 1 + minDepth(root.right);
103+
return 1 + dfs(root.right);
97104
}
98105
if (root.right == null) {
99-
return 1 + minDepth(root.left);
106+
return 1 + dfs(root.left);
100107
}
101-
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
108+
return 1 + Math.min(dfs(root.left), dfs(root.right));
102109
}
103110
}
104111
```
@@ -119,10 +126,13 @@ class Solution {
119126
* @return {number}
120127
*/
121128
var minDepth = function (root) {
122-
if (root == null) return 0;
123-
if (root.left == null) return minDepth(root.right) + 1;
124-
if (root.right == null) return minDepth(root.left) + 1;
125-
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
129+
function dfs(root) {
130+
if (!root) return 0;
131+
if (!root.left) return 1 + dfs(root.right);
132+
if (!root.right) return 1 + dfs(root.left);
133+
return 1 + Math.min(dfs(root.left), dfs(root.right));
134+
}
135+
return dfs(root);
126136
};
127137
```
128138

@@ -143,20 +153,54 @@ var minDepth = function (root) {
143153
class Solution {
144154
public:
145155
int minDepth(TreeNode* root) {
146-
if (root == nullptr) {
147-
return 0;
148-
}
149-
if (root->left == nullptr) {
150-
return 1 + minDepth(root->right);
151-
}
152-
if (root->right == nullptr) {
153-
return 1 + minDepth(root->left);
154-
}
155-
return 1 + min(minDepth(root->left), minDepth(root->right));
156+
return dfs(root);
157+
}
158+
159+
int dfs(TreeNode* root) {
160+
if (!root) return 0;
161+
if (!root->left) return 1 + dfs(root->right);
162+
if (!root->right) return 1 + dfs(root->left);
163+
return 1 + min(dfs(root->left), dfs(root->right));
156164
}
157165
};
158166
```
159167

168+
### **Go**
169+
170+
```go
171+
/**
172+
* Definition for a binary tree node.
173+
* type TreeNode struct {
174+
* Val int
175+
* Left *TreeNode
176+
* Right *TreeNode
177+
* }
178+
*/
179+
func minDepth(root *TreeNode) int {
180+
var dfs func(root *TreeNode) int
181+
dfs = func(root *TreeNode) int {
182+
if root == nil {
183+
return 0
184+
}
185+
if root.Left == nil {
186+
return 1 + dfs(root.Right)
187+
}
188+
if root.Right == nil {
189+
return 1 + dfs(root.Left)
190+
}
191+
return 1 + min(dfs(root.Left), dfs(root.Right))
192+
}
193+
return dfs(root)
194+
}
195+
196+
func min(a, b int) int {
197+
if a < b {
198+
return a
199+
}
200+
return b
201+
}
202+
```
203+
160204
### **...**
161205

162206
```

0 commit comments

Comments
 (0)