LeetCode刷题实战111:二叉树的最小深度
程序IT圈
共 1697字,需浏览 4分钟
·
2020-11-30 22:34
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. Note: A leaf is a node with no children.
题意
解题
public class Solution {
public int minDepth(TreeNode root) {
if(root!=null){
int left=Integer.MAX_VALUE;
int right=Integer.MAX_VALUE;
if(root.left!=null){
left=minDepth(root.left);
}
if(root.right!=null){
right=minDepth(root.right);
}
if(leftreturn left+1;
}
else if(left>right){
return right+1;
}
else if(left==right&&left!=Integer.MAX_VALUE){
return left+1;
}
else{
return 1;
}
}
return 0;
}
}
评论