Skip to content

Commit d019c94

Browse files
committed
feat: add solutions to lc problem: No.0111
No.0111.Minimum Depth of Binary Tree
1 parent 1375467 commit d019c94

File tree

5 files changed

+355
-52
lines changed

5 files changed

+355
-52
lines changed

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

+132-26
Original file line numberDiff line numberDiff line change
@@ -110,32 +110,6 @@ class Solution {
110110
}
111111
```
112112

113-
### **JavaScript**
114-
115-
```js
116-
/**
117-
* Definition for a binary tree node.
118-
* function TreeNode(val, left, right) {
119-
* this.val = (val===undefined ? 0 : val)
120-
* this.left = (left===undefined ? null : left)
121-
* this.right = (right===undefined ? null : right)
122-
* }
123-
*/
124-
/**
125-
* @param {TreeNode} root
126-
* @return {number}
127-
*/
128-
var minDepth = function (root) {
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);
136-
};
137-
```
138-
139113
### **C++**
140114

141115
```cpp
@@ -201,6 +175,138 @@ func min(a, b int) int {
201175
}
202176
```
203177

178+
### **JavaScript**
179+
180+
```js
181+
/**
182+
* Definition for a binary tree node.
183+
* function TreeNode(val, left, right) {
184+
* this.val = (val===undefined ? 0 : val)
185+
* this.left = (left===undefined ? null : left)
186+
* this.right = (right===undefined ? null : right)
187+
* }
188+
*/
189+
/**
190+
* @param {TreeNode} root
191+
* @return {number}
192+
*/
193+
var minDepth = function (root) {
194+
function dfs(root) {
195+
if (!root) return 0;
196+
if (!root.left) return 1 + dfs(root.right);
197+
if (!root.right) return 1 + dfs(root.left);
198+
return 1 + Math.min(dfs(root.left), dfs(root.right));
199+
}
200+
return dfs(root);
201+
};
202+
```
203+
204+
### **TypeScript**
205+
206+
```ts
207+
/**
208+
* Definition for a binary tree node.
209+
* class TreeNode {
210+
* val: number
211+
* left: TreeNode | null
212+
* right: TreeNode | null
213+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
214+
* this.val = (val===undefined ? 0 : val)
215+
* this.left = (left===undefined ? null : left)
216+
* this.right = (right===undefined ? null : right)
217+
* }
218+
* }
219+
*/
220+
221+
function minDepth(root: TreeNode | null): number {
222+
if (root == null) {
223+
return 0;
224+
}
225+
const { left, right } = root;
226+
if (left == null) {
227+
return 1 + minDepth(right);
228+
}
229+
if (right == null) {
230+
return 1 + minDepth(left);
231+
}
232+
return 1 + Math.min(minDepth(left), minDepth(right));
233+
}
234+
```
235+
236+
### **Rust**
237+
238+
```rust
239+
// Definition for a binary tree node.
240+
// #[derive(Debug, PartialEq, Eq)]
241+
// pub struct TreeNode {
242+
// pub val: i32,
243+
// pub left: Option<Rc<RefCell<TreeNode>>>,
244+
// pub right: Option<Rc<RefCell<TreeNode>>>,
245+
// }
246+
//
247+
// impl TreeNode {
248+
// #[inline]
249+
// pub fn new(val: i32) -> Self {
250+
// TreeNode {
251+
// val,
252+
// left: None,
253+
// right: None
254+
// }
255+
// }
256+
// }
257+
use std::rc::Rc;
258+
use std::cell::RefCell;
259+
impl Solution {
260+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
261+
if root.is_none() {
262+
return 0;
263+
}
264+
let node = root.as_ref().unwrap().borrow();
265+
if node.left.is_none() {
266+
return 1 + Self::dfs(&node.right);
267+
}
268+
if node.right.is_none() {
269+
return 1 + Self::dfs(&node.left);
270+
}
271+
1 + Self::dfs(&node.left).min(Self::dfs(&node.right))
272+
}
273+
274+
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
275+
Self::dfs(&root)
276+
}
277+
}
278+
```
279+
280+
### **C**
281+
282+
```c
283+
/**
284+
* Definition for a binary tree node.
285+
* struct TreeNode {
286+
* int val;
287+
* struct TreeNode *left;
288+
* struct TreeNode *right;
289+
* };
290+
*/
291+
292+
#define min(a,b) (((a) < (b)) ? (a) : (b))
293+
294+
int minDepth(struct TreeNode *root) {
295+
if (!root) {
296+
return 0;
297+
}
298+
if (!root->left) {
299+
return 1 + minDepth(root->right);
300+
}
301+
if (!root->right) {
302+
return 1 + minDepth(root->left);
303+
}
304+
int left = minDepth(root->left);
305+
int right = minDepth(root->right);
306+
return 1 + min(left, right);
307+
}
308+
```
309+
204310
### **...**
205311
206312
```

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

+132-26
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,6 @@ class Solution {
9898
}
9999
```
100100

101-
### **JavaScript**
102-
103-
```js
104-
/**
105-
* Definition for a binary tree node.
106-
* function TreeNode(val, left, right) {
107-
* this.val = (val===undefined ? 0 : val)
108-
* this.left = (left===undefined ? null : left)
109-
* this.right = (right===undefined ? null : right)
110-
* }
111-
*/
112-
/**
113-
* @param {TreeNode} root
114-
* @return {number}
115-
*/
116-
var minDepth = function (root) {
117-
function dfs(root) {
118-
if (!root) return 0;
119-
if (!root.left) return 1 + dfs(root.right);
120-
if (!root.right) return 1 + dfs(root.left);
121-
return 1 + Math.min(dfs(root.left), dfs(root.right));
122-
}
123-
return dfs(root);
124-
};
125-
```
126-
127101
### **C++**
128102

129103
```cpp
@@ -189,6 +163,138 @@ func min(a, b int) int {
189163
}
190164
```
191165

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 minDepth = function (root) {
182+
function dfs(root) {
183+
if (!root) return 0;
184+
if (!root.left) return 1 + dfs(root.right);
185+
if (!root.right) return 1 + dfs(root.left);
186+
return 1 + Math.min(dfs(root.left), dfs(root.right));
187+
}
188+
return dfs(root);
189+
};
190+
```
191+
192+
### **TypeScript**
193+
194+
```ts
195+
/**
196+
* Definition for a binary tree node.
197+
* class TreeNode {
198+
* val: number
199+
* left: TreeNode | null
200+
* right: TreeNode | null
201+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
202+
* this.val = (val===undefined ? 0 : val)
203+
* this.left = (left===undefined ? null : left)
204+
* this.right = (right===undefined ? null : right)
205+
* }
206+
* }
207+
*/
208+
209+
function minDepth(root: TreeNode | null): number {
210+
if (root == null) {
211+
return 0;
212+
}
213+
const { left, right } = root;
214+
if (left == null) {
215+
return 1 + minDepth(right);
216+
}
217+
if (right == null) {
218+
return 1 + minDepth(left);
219+
}
220+
return 1 + Math.min(minDepth(left), minDepth(right));
221+
}
222+
```
223+
224+
### **Rust**
225+
226+
```rust
227+
// Definition for a binary tree node.
228+
// #[derive(Debug, PartialEq, Eq)]
229+
// pub struct TreeNode {
230+
// pub val: i32,
231+
// pub left: Option<Rc<RefCell<TreeNode>>>,
232+
// pub right: Option<Rc<RefCell<TreeNode>>>,
233+
// }
234+
//
235+
// impl TreeNode {
236+
// #[inline]
237+
// pub fn new(val: i32) -> Self {
238+
// TreeNode {
239+
// val,
240+
// left: None,
241+
// right: None
242+
// }
243+
// }
244+
// }
245+
use std::rc::Rc;
246+
use std::cell::RefCell;
247+
impl Solution {
248+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
249+
if root.is_none() {
250+
return 0;
251+
}
252+
let node = root.as_ref().unwrap().borrow();
253+
if node.left.is_none() {
254+
return 1 + Self::dfs(&node.right);
255+
}
256+
if node.right.is_none() {
257+
return 1 + Self::dfs(&node.left);
258+
}
259+
1 + Self::dfs(&node.left).min(Self::dfs(&node.right))
260+
}
261+
262+
pub fn min_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
263+
Self::dfs(&root)
264+
}
265+
}
266+
```
267+
268+
### **C**
269+
270+
```c
271+
/**
272+
* Definition for a binary tree node.
273+
* struct TreeNode {
274+
* int val;
275+
* struct TreeNode *left;
276+
* struct TreeNode *right;
277+
* };
278+
*/
279+
280+
#define min(a,b) (((a) < (b)) ? (a) : (b))
281+
282+
int minDepth(struct TreeNode *root) {
283+
if (!root) {
284+
return 0;
285+
}
286+
if (!root->left) {
287+
return 1 + minDepth(root->right);
288+
}
289+
if (!root->right) {
290+
return 1 + minDepth(root->left);
291+
}
292+
int left = minDepth(root->left);
293+
int right = minDepth(root->right);
294+
return 1 + min(left, right);
295+
}
296+
```
297+
192298
### **...**
193299
194300
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
#define min(a,b) (((a) < (b)) ? (a) : (b))
11+
12+
int minDepth(struct TreeNode *root) {
13+
if (!root) {
14+
return 0;
15+
}
16+
if (!root->left) {
17+
return 1 + minDepth(root->right);
18+
}
19+
if (!root->right) {
20+
return 1 + minDepth(root->left);
21+
}
22+
int left = minDepth(root->left);
23+
int right = minDepth(root->right);
24+
return 1 + min(left, right);
25+
}

0 commit comments

Comments
 (0)