forked from dnshi/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumDepthOfBinaryTree.js
More file actions
31 lines (29 loc) · 901 Bytes
/
MinimumDepthOfBinaryTree.js
File metadata and controls
31 lines (29 loc) · 901 Bytes
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
// Source : https://leetcode.com/problems/minimum-depth-of-binary-tree
// Author : Dean Shi
// Date : 2017-03-11
/***************************************************************************************
*
* 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.
*
*
***************************************************************************************/
/**
* 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
if (!root.left) return minDepth(root.right) + 1
if (!root.right) return minDepth(root.left) + 1
return Math.min(minDepth(root.left), minDepth(root.right)) + 1
};