111. 二叉树的最小深度
递归解法
public class Solution {// 可以用 Math.min(左子树的深度,右子树的深度) + 1// 但是有一点要注意,当左右子树都不空空的时候是没问题的// 但如果左右子树有一个节点为空就会有问题,因为空的那个节点深度是0,不能用这个0作为最小值返回// 如果左子树为空则返回右子树的最小深度,右子树同理public int minDepth(TreeNode root) {// 如果为空返回 0if (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) + 1, minDepth(root.right) + 1);}}

