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 发布!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。