和T124,T687都是一种题
class Solution {
int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
if(root == null) return 0;
dfs(root);
return max;
}
public int dfs(TreeNode root){
if(root.left == null && root.right == null) return 0;
int left = root.left == null ? 0 : dfs(root.left) + 1;
int right = root.right == null ? 0 : dfs(root.right) + 1;
max = Math.max(max, left + right);
return Math.max(left, right);
}
}~~~