-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMInDepthBinaryTree.js
More file actions
34 lines (30 loc) · 1.08 KB
/
MInDepthBinaryTree.js
File metadata and controls
34 lines (30 loc) · 1.08 KB
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
/**
* 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)
* } */
var minDepth = function (root) {
//recursive
if (!root) return 0;
else if (!root.left && !root.right) return 1;
else if (!root.left) return 1 + minDepth(root.right);
else if (!root.right) return 1 + minDepth(root.left);
else return 1 + Math.min(minDepth(root.left), minDepth(root.right));
//Iterative approach
// if (!root) return 0;
// const que = [root];
// let minDepth = 1;
// while (que.length > 0) {
// let level = que.length;
// for (let i = 0; i < level; i++) {
// const curr = que.shift();
// if (!curr.left && !curr.right) return minDepth;
// if (curr.left) que.push(curr.left);
// if (curr.right) que.push(curr.right);
// }
// minDepth++;
// }
// return minDepth;
};