1.最大深度
原题目
public class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int a = 0, b = 0;
if (root.left != null) {
a = maxDepth(root.left) + 1;
}
if (root.right != null) {
b = maxDepth(root.right) + 1;
}
if (a == 0 && b == 0) {
return 1;
}
if (a > b) {
return a;
} else {
return b;
}
}
}
2.最小深度
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.right), minDepth(root.left)) + 1;
}