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===null)return 0;
var left=minDepth(root.left);
var right=minDepth(root.right);
if(left===0)return right+1;
if(right===0)return left+1;
return (left<right)?(left+1):(right+1)
};