/
111.minimum-depth-of-binary-tree.js
71 lines (68 loc) · 1.38 KB
/
111.minimum-depth-of-binary-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* @lc app=leetcode id=111 lang=javascript
*
* [111] Minimum Depth of Binary Tree
*
* https://leetcode.com/problems/minimum-depth-of-binary-tree/description/
*
* algorithms
* Easy (35.07%)
* Total Accepted: 285.1K
* Total Submissions: 812.5K
* Testcase Example: '[3,9,20,null,null,15,7]'
*
* Given a binary tree, find its minimum depth.
*
* The minimum depth is the number of nodes along the shortest path from the
* root node down to the nearest leaf node.
*
* Note: A leaf is a node with no children.
*
* Example:
* 0 1 2 3 4 5 6
* 1 2 2 3 3 4 4
* Given binary tree [3,9,20,null,null,15,7],
*
*
* 3
* / \
* 9 20
* / \
* 15 7
*
* return its minimum depth = 2.
*
*/
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var minDepth = function(root) {
if (!root) return 0;
let min = Number.MAX_VALUE;
let s = [];
let d = [];
s.push(root);
d.push(1);
while (s.length > 0) {
let i = s.pop();
let dep = d.pop();
if (!i.left && !i.right && dep < min) min = dep;
if (!!i.left) {
s.push(i.left);
d.push(dep + 1);
}
if (!!i.right) {
s.push(i.right);
d.push(dep + 1);
}
}
return min;
};