

class Solution5 {
public int maxDepth(TreeNode2 root) {
if(root==null){
return 0;//退出条件
}
//单层逻辑/左子树的深度和右子树的深度最大的值+1就是二叉树的深度
int left = maxDepth(root.left);
int right=maxDepth(root.right);
return Math.max(left,right)+1;
}
}
class Solution5 {
public int maxDepth(TreeNode2 root) {
if(root==null){
return 0;//退出条件
}
//单层逻辑/左子树的深度和右子树的深度最大的值+1就是二叉树的深度
int left = maxDepth(root.left);
int right=maxDepth(root.right);
return Math.max(left,right)+1;
}
}
本文标题:二叉树最大深度
本文链接:https://www.haomeiwen.com/subject/virbcrtx.html
网友评论