110. 平衡二叉树 - 力扣(LeetCode)
int TreeDepth(struct TreeNode* root) {
if(root==NULL)
return 0;
int leftDepth=TreeDepth(root->left);
int rightDepth=TreeDepth(root->right);
return leftDepth>rightDepth?leftDepth+1:rightDepth+1;
}
//最好是O(N),最坏是O(N*N)
bool isBalanced(struct TreeNode* root) {
if(root==NULL)
return true;
int gap=TreeDepth(root->left)-TreeDepth(root->right);
if(abs(gap)>1)
return false;
return isBalanced(root->left)&&isBalanced(root->right);
}