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;
}
www.mianshi.online,www.i9code.cn
本文由博客一文多发平台 OpenWrite 发布!