class Solution {
int max=0;
public int diameterOfBinaryTree(TreeNode root) {
depth(root);
return max;
}
public int depth(TreeNode root){
if(root==null){
return 0;
}
int leftdepth=depth(root.left);
int rightdepth=depth(root.right);
max = Math.max(leftdepth + rightdepth, max);
return Math.max(leftdepth, rightdepth) + 1;
}
}
leetcode:19.二叉树的直径 简单
最新推荐文章于 2024-09-08 20:02:08 发布