111. Minimum Depth of Binary Tree
- Total Accepted: 121443
- Total Submissions: 386887
- Difficulty: Easy
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.
Subscribe to see which companies asked this question
注意:
if条件,逻辑不要重复
代码:
public class Solution {
public int minDepth(TreeNode root) {
if(root == null) return 0;
if(root != null && root.right == null && root.left == null) return 1;
if(root.right == null) return minDepth(root.left)+1;
if(root.left == null) return minDepth(root.right)+1;
else
return minDepth(root.left) < minDepth(root.right) ? minDepth(root.left)+1 : minDepth(root.right)+1;
}
}