给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
class Solution {
public int maxDepth(TreeNode root
) {
if(root
== null
){
return 0;
}
if(root
.left
== null
&& root
.right
== null
){
return 1;
}
int maxL
= Math
.max(maxDepth(root
.left
),maxDepth(root
.right
))+1;
return maxL
;
}
}
转载请注明原文地址:https://tech.qufami.com/read-29175.html