Skip to content

Commit 7b8ae19

Browse files
committed
feat: add solution of Minimum Depth of Binary Tree(111) with javascript.
1 parent 22f80c1 commit 7b8ae19

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
| [107][107-question] | [Binary Tree Level Order Traversal II][107-tips] | [Easy][E] | [][107-java] | [][107-js] | [][107-kotlin] |
8080
| [108][108-question] | [Convert Sorted Array to Binary Search Tree][108-tips] | [Easy][E] | [][108-java] | [][108-js] | [][108-kotlin] |
8181
| [110][110-question] | [Balanced Binary Tree][110-tips] | [Easy][E] | [][110-java] | [][110-js] | [][110-kotlin] |
82-
| [111][111-question] | [Minimum Depth of Binary Tree][111-tips] | [Easy][E] | [][111-java] | | [][111-kotlin] |
82+
| [111][111-question] | [Minimum Depth of Binary Tree][111-tips] | [Easy][E] | [][111-java] | [][111-js] | [][111-kotlin] |
8383
| [112][112-question] | [Path Sum][112-tips] | [Easy][E] | [][112-java] | | [][112-kotlin] |
8484
| [118][118-question] | [Pascal's Triangle][118-tips] | [Easy][E] | [][118-java] | | [][118-kotlin] |
8585
| [119][119-question] | [Pascal's Triangle II][119-tips] | [Easy][E] | [][119-java] | | [][119-kotlin] |
@@ -459,6 +459,7 @@ commit信息模板: ``feat: add the solution of `Two Sum`(001) with Java``
459459
[107-js]: ./src/_107/Solution.js
460460
[108-js]: ./src/_108/Solution.js
461461
[110-js]: ./src/_110/Solution.js
462+
[111-js]: ./src/_111/Solution.js
462463
[226-js]: ./src/_226/Solution.js
463464
[561-js]: ./src/_561/Solution.js
464465
[643-js]: ./src/_643/Solution.js

src/_111/Solution.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
var minDepth = function(root) {
13+
if(root == null) return 0
14+
if(root.left === null && root.right === null) {return 1}
15+
if(root.left === null && root.right !== null) {return 1 + minDepth(root.right)}
16+
if(root.left !== null && root.right === null) {return 1 + minDepth(root.left)}
17+
return 1 + Math.min(minDepth(root.left),minDepth(root.right))
18+
};

tips/111/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ class Solution {
4848
}
4949
```
5050

51+
```JavaScript
52+
var minDepth = function(root) {
53+
if(root == null) return 0
54+
if(root.left === null && root.right === null) {return 1}
55+
if(root.left === null && root.right !== null) {return 1 + minDepth(root.right)}
56+
if(root.left !== null && root.right === null) {return 1 + minDepth(root.left)}
57+
return 1 + Math.min(minDepth(root.left),minDepth(root.right))
58+
};
59+
```
5160
## 思路 1
5261

5362
第二种思路就是利用宽搜了,搜索到该层有叶子节点,那就返回该层宽度即可。

0 commit comments

Comments
 (0)