题目传送:https://leetcode.cn/problems/maximum-depth-of-binary-tree/
运行效率
代码如下
public int maxDepth(TreeNode root) {
//处理边界情况
if(root==null){
return 0;
}
//如果是叶子节点
if(root.left==null&&root.right==null){
return 1;
}
int leftHeight = maxDepth(root.left);
int rightHeight = maxDepth(root.right);
return Math.max(leftHeight, rightHeight)+1;
}