您的位置:首页 > 编程语言 > Java开发

[Leetcode] Minimum Depth of Binary Tree (Java)

2014-01-20 18:34 369 查看
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.

找到最近叶子节点的高度

public class Solution {
public int minDepth(TreeNode root) {
if(root==null)
return 0;

if(root.left==null&&root.right==null)
return 1;

int left = 0;
int right =0;
if(root.left!=null)
left = minDepth(root.left);
if(root.right!=null)
right = minDepth(root.right);
if(right!=0&&left!=0)
return 1+Math.min(left, right);
if(right!=0)
return 1+right;
return 1+left;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: