给定一个二叉树,找出其最小深度。 示例: 12345678 给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20 / 15 7返回它的最小深度 2. 代码实现: 123456789101112 public int minDepth(TreeNode root) { if(root == null){ return 0; } if(root.left == null){ return minDepth(root.right)+1; } if(root.right == null){ return minDepth(root.left) + 1; } return Math.min(minDepth(root.left),minDepth(root.right)) + 1; } 赞微海报分享
近期评论