/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int res;
public int diameterOfBinaryTree(TreeNode root) {
res = 1;
maxHeigth(root);
return res - 1;
}
public int maxHeigth(TreeNode root)
{
if(root == null) return 0;
int l = maxHeigth(root.left);
int r = maxHeigth(root.right);
res = Math.max(res, l+r+1);
return 1 + Math.max(l,r);
}
}
543. 二叉树的直径
最新推荐文章于 2021-12-22 10:26:51 发布