二叉树的直径
class Solution {
int sum;
int post(TreeNode *root) {
if (!root)
return 0;
int left = post(root->left);
int right = post(root->right);
sum = max(sum, left + right);
return max(left, right) + 1;
}
public:
int diameterOfBinaryTree(TreeNode *root) {
sum = 0;
post(root);
return sum;
}
};