int maxVal;
public int diameterOfBinaryTree(TreeNode root) {
if (root == null) return 0;
getNodeCountonDiameter(root);
return maxVal - 1;
}
public int getNodeCountonDiameter(TreeNode root) {
if (root == null) return 0;
int left = getNodeCountonDiameter(root.left);
int right = getNodeCountonDiameter(root.right);
maxVal = Math.max(left + right + 1, maxVal);
return Math.max(left, right) + 1;
}
转载请注明原文地址:https://ipadbbs.8miu.com/read-18056.html