Skip to content

Commit b5fe668

Browse files
committed
feat: add solutions to lc problems: No.2078~2081
1 parent 65e5155 commit b5fe668

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2266
-371
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
## 站点
2222

2323
- Netlify: https://lc.netlify.app
24-
- Gitee Pages: https://doocs.gitee.io/leetcode
2524
- GitHub Pages: https://doocs.github.io/leetcode
2625

2726
## LeetCode 全解

README_EN.md

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
2121
## Sites
2222

2323
- Netlify: https://lc.netlify.app
24-
- Gitee Pages: https://doocs.gitee.io/leetcode
2524
- GitHub Pages: https://doocs.github.io/leetcode
2625

2726
## Solutions

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

+69-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
<p>返回它的最大深度&nbsp;3 。</p>
2525

26-
2726
## 解法
2827

2928
<!-- 这里可写通用的实现逻辑 -->
@@ -47,8 +46,7 @@ class Solution:
4746
def maxDepth(self, root: TreeNode) -> int:
4847
if root is None:
4948
return 0
50-
l = self.maxDepth(root.left)
51-
r = self.maxDepth(root.right)
49+
l, r = self.maxDepth(root.left), self.maxDepth(root.right)
5250
return 1 + max(l, r)
5351
```
5452

@@ -74,7 +72,9 @@ class Solution:
7472
*/
7573
class Solution {
7674
public int maxDepth(TreeNode root) {
77-
if (root == null) return 0;
75+
if (root == null) {
76+
return 0;
77+
}
7878
int l = maxDepth(root.left);
7979
int r = maxDepth(root.right);
8080
return 1 + Math.max(l, r);
@@ -85,14 +85,76 @@ class Solution {
8585
### **C++**
8686

8787
```cpp
88+
/**
89+
* Definition for a binary tree node.
90+
* struct TreeNode {
91+
* int val;
92+
* TreeNode *left;
93+
* TreeNode *right;
94+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
95+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
96+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
97+
* };
98+
*/
8899
class Solution {
89100
public:
90101
int maxDepth(TreeNode* root) {
91102
if (!root) return 0;
92-
int l = maxDepth(root->left);
93-
int r = maxDepth(root->right);
94-
return max(l, r) + 1;
103+
int l = maxDepth(root->left), r = maxDepth(root->right);
104+
return 1 + max(l, r);
105+
}
106+
};
107+
```
108+
109+
### **Go**
110+
111+
```go
112+
/**
113+
* Definition for a binary tree node.
114+
* type TreeNode struct {
115+
* Val int
116+
* Left *TreeNode
117+
* Right *TreeNode
118+
* }
119+
*/
120+
func maxDepth(root *TreeNode) int {
121+
if root == nil {
122+
return 0
123+
}
124+
l, r := maxDepth(root.Left), maxDepth(root.Right)
125+
return 1 + max(l, r)
126+
}
127+
128+
func max(a, b int) int {
129+
if a > b {
130+
return a
131+
}
132+
return b
133+
}
134+
```
135+
136+
### **JavaScript**
137+
138+
```js
139+
/**
140+
* Definition for a binary tree node.
141+
* function TreeNode(val, left, right) {
142+
* this.val = (val===undefined ? 0 : val)
143+
* this.left = (left===undefined ? null : left)
144+
* this.right = (right===undefined ? null : right)
145+
* }
146+
*/
147+
/**
148+
* @param {TreeNode} root
149+
* @return {number}
150+
*/
151+
var maxDepth = function(root) {
152+
if (!root) {
153+
return 0;
95154
}
155+
const l = maxDepth(root.left);
156+
const r = maxDepth(root.right);
157+
return 1 + Math.max(l, r);
96158
};
97159
```
98160

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

+69-26
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66

77
<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p>
88

9-
10-
119
<p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

1713
<p><strong>Example 1:</strong></p>
@@ -26,12 +22,8 @@
2622

2723
</pre>
2824

29-
30-
3125
<p><strong>Example 2:</strong></p>
3226

33-
34-
3527
<pre>
3628

3729
<strong>Input:</strong> root = [1,null,2]
@@ -40,12 +32,8 @@
4032

4133
</pre>
4234

43-
44-
4535
<p><strong>Example 3:</strong></p>
4636

47-
48-
4937
<pre>
5038

5139
<strong>Input:</strong> root = []
@@ -54,12 +42,8 @@
5442

5543
</pre>
5644

57-
58-
5945
<p><strong>Example 4:</strong></p>
6046

61-
62-
6347
<pre>
6448

6549
<strong>Input:</strong> root = [0]
@@ -68,14 +52,10 @@
6852

6953
</pre>
7054

71-
72-
7355
<p>&nbsp;</p>
7456

7557
<p><strong>Constraints:</strong></p>
7658

77-
78-
7959
<ul>
8060
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
8161
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
@@ -98,8 +78,7 @@ class Solution:
9878
def maxDepth(self, root: TreeNode) -> int:
9979
if root is None:
10080
return 0
101-
l = self.maxDepth(root.left)
102-
r = self.maxDepth(root.right)
81+
l, r = self.maxDepth(root.left), self.maxDepth(root.right)
10382
return 1 + max(l, r)
10483
```
10584

@@ -123,7 +102,9 @@ class Solution:
123102
*/
124103
class Solution {
125104
public int maxDepth(TreeNode root) {
126-
if (root == null) return 0;
105+
if (root == null) {
106+
return 0;
107+
}
127108
int l = maxDepth(root.left);
128109
int r = maxDepth(root.right);
129110
return 1 + Math.max(l, r);
@@ -134,14 +115,76 @@ class Solution {
134115
### **C++**
135116

136117
```cpp
118+
/**
119+
* Definition for a binary tree node.
120+
* struct TreeNode {
121+
* int val;
122+
* TreeNode *left;
123+
* TreeNode *right;
124+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
125+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
126+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
127+
* };
128+
*/
137129
class Solution {
138130
public:
139131
int maxDepth(TreeNode* root) {
140132
if (!root) return 0;
141-
int l = maxDepth(root->left);
142-
int r = maxDepth(root->right);
143-
return max(l, r) + 1;
133+
int l = maxDepth(root->left), r = maxDepth(root->right);
134+
return 1 + max(l, r);
135+
}
136+
};
137+
```
138+
139+
### **Go**
140+
141+
```go
142+
/**
143+
* Definition for a binary tree node.
144+
* type TreeNode struct {
145+
* Val int
146+
* Left *TreeNode
147+
* Right *TreeNode
148+
* }
149+
*/
150+
func maxDepth(root *TreeNode) int {
151+
if root == nil {
152+
return 0
153+
}
154+
l, r := maxDepth(root.Left), maxDepth(root.Right)
155+
return 1 + max(l, r)
156+
}
157+
158+
func max(a, b int) int {
159+
if a > b {
160+
return a
161+
}
162+
return b
163+
}
164+
```
165+
166+
### **JavaScript**
167+
168+
```js
169+
/**
170+
* Definition for a binary tree node.
171+
* function TreeNode(val, left, right) {
172+
* this.val = (val===undefined ? 0 : val)
173+
* this.left = (left===undefined ? null : left)
174+
* this.right = (right===undefined ? null : right)
175+
* }
176+
*/
177+
/**
178+
* @param {TreeNode} root
179+
* @return {number}
180+
*/
181+
var maxDepth = function(root) {
182+
if (!root) {
183+
return 0;
144184
}
185+
const l = maxDepth(root.left);
186+
const r = maxDepth(root.right);
187+
return 1 + Math.max(l, r);
145188
};
146189
```
147190

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
112
class Solution {
213
public:
314
int maxDepth(TreeNode* root) {
415
if (!root) return 0;
5-
int l = maxDepth(root->left);
6-
int r = maxDepth(root->right);
7-
return max(l, r) + 1;
16+
int l = maxDepth(root->left), r = maxDepth(root->right);
17+
return 1 + max(l, r);
818
}
919
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func maxDepth(root *TreeNode) int {
10+
if root == nil {
11+
return 0
12+
}
13+
l, r := maxDepth(root.Left), maxDepth(root.Right)
14+
return 1 + max(l, r)
15+
}
16+
17+
func max(a, b int) int {
18+
if a > b {
19+
return a
20+
}
21+
return b
22+
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616
class Solution {
1717
public int maxDepth(TreeNode root) {
18-
if (root == null) return 0;
18+
if (root == null) {
19+
return 0;
20+
}
1921
int l = maxDepth(root.left);
2022
int r = maxDepth(root.right);
2123
return 1 + Math.max(l, r);

0 commit comments

Comments
 (0)