二叉树的最小深度
文章目录
public int minDepth(TreeNode root) {
if(root==null){
return 0;
}
// 1
// /
// 2
//最小深度是 1
if(root.right==null){
return 1+minDepth(root.left);
}
// 1
// \
// 2
//的最小深度是 1
if(root.left==null){
return 1+minDepth(root.right);
}
return Math.min(minDepth(root.left),minDepth(root.right))+1;
}https://www.mianshi.online,https://www.i9code.cn
本文由博客一文多发平台 OpenWrite 发布!
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论