-
-
Notifications
You must be signed in to change notification settings - Fork 422
/
111-minimum-depth-of-binary-tree.js
56 lines (51 loc) · 1.35 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
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
// PROBLEM:
// 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.
// test cases:
// [3,9,20,null,null,15,7] -- output: 2
// [2,null,3,null,4,null,5,null,6] -- output: 5
// [0] -- output: 1
// [] -- output: 0
var minDepth = function(root) {
if(root) {
return traversal(root, 1);
} else { return 0 }
};
// traversal of the tree is recursive to ensure travel down
// both the right and left children nodes
var traversal = (root, depth) => {
let current = root;
let right;
let left;
if(!current) {
return null;
// this checks if the current node is a leaf node
} if (!current.right && !current.left) {
return depth;
}
right = traversal(current.right, depth+1)
left = traversal(current.left, depth+1)
if(!right) {
return left;
} else if(!left) {
return right;
} else {
if(right < left) {
return right
} else {
return left
}
}
}