We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 67a5d35 commit 1c40da0Copy full SHA for 1c40da0
111-minimum-depth-of-binary-tree.js
@@ -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