Skip to content

Commit 1c40da0

Browse files
authored
Create 111-minimum-depth-of-binary-tree.js
1 parent 67a5d35 commit 1c40da0

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

111-minimum-depth-of-binary-tree.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
const minDepth = function(root) {
13+
if(root == null) return 0
14+
if(root.left === null && root.right === null) return 1
15+
const res = {
16+
min:Number.MAX_VALUE
17+
}
18+
dfs(root, res, 1)
19+
return res.min
20+
};
21+
22+
function dfs(node, res, cur) {
23+
if(node == null) return
24+
if(node !== null && node.left === null && node.right === null) {
25+
if(cur < res.min) {
26+
res.min = cur
27+
}
28+
return
29+
}
30+
dfs(node.left, res, cur + 1)
31+
dfs(node.right, res, cur + 1)
32+
33+
}

0 commit comments

Comments
 (0)