
首先可以想到使用深度优先搜索的方法,遍历整棵树,记录最小深度。 对于每一个非叶子节点,我们只需要分别计算其左右子树的最小叶子节点深度。这样就将一个大问题转化为了小问题,可以递归地解决该问题。
若 root 为null 返回 0 若 为 叶子节点 则返回1,作为递归的终止条件。 接着去找到左右子树的最小叶子节点的深度。最终得到结果
class Solution {
public int minDepth(TreeNode root) {
if(root == null){
return 0;
}
if(root.left == null && root.right == null){
return 1;
}
int min = Integer.MAX_VALUE;
if(root.left != null){
min = Math.min(minDepth(root.left),min);
}
if(root.right != null){
min = Math.min(minDepth(root.right),min);
}
return min+1;
}
}时间复杂度:O(N),其中 N 是树的节点数。对每个节点访问一次。 空间复杂度:O(H),其中 H 是树的高度。空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为 O(logN)。