1181 · 二叉树的直径
方法1:DFS
//1.经过根节点,左右子树的最大深度相加
//2.不经过根节点,找出根节点的左子树或者根节点的右子树作为根节点的最长路径
int maxv = 0;
public int diameterOfBinaryTree(TreeNode root) {
// write your code here
dfs(root);
return maxv;
}
private int dfs(TreeNode root) {
if (root == null) return 0;
int left = dfs(root.left);
int right = dfs(root.right);
maxv = Math.max(maxv, left + right);
return Math.max(left, right) + 1;
}