LeetCode-543-二叉树的直径
思路
其实就是求二叉树的左右子树高度和,只要取最大值就好。
代码
int m=0;
public int height(TreeNode root){
if(root==null)return 0;
int L=height(root.left);
int R=height(root.right);
m=Math.max(m,L+R);
return Math.max(L,R)+1;
}
public int diameterOfBinaryTree(TreeNode root) {
height(root);
return m;
}