Leetcode104. 二叉树的最大深度

霍华德

关注

阅读 72

2022-09-03


题目传送:​​https://leetcode.cn/problems/maximum-depth-of-binary-tree/​​

运行效率

Leetcode104. 二叉树的最大深度_数据结构

代码如下

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;
}


精彩评论(0)

0 0 举报