0
点赞
收藏
分享

微信扫一扫

Leetcode104. 二叉树的最大深度


题目传送:​​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 条评论