您的位置:首页 > 其它

111. Minimum Depth of Binary Tree

2017-04-19 23:57 134 查看
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)

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: