计算一颗二叉树的最大深度和最小深度
public int maxDepth(TreeNode root){
if(root == null){
return 0;
}
return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
}
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;
}
未完待续。。。。。
网友评论