mirror of
https://github.com/TheAlgorithms/C
synced 2025-04-20 20:32:50 +03:00
20 lines
450 B
C
20 lines
450 B
C
int max(int a, int b) {
|
|
return a >= b ? a : b;
|
|
}
|
|
|
|
int height(struct TreeNode* root) {
|
|
if (root == NULL)
|
|
return 0;
|
|
else
|
|
return 1 + max(height(root->left), height(root->right));
|
|
}
|
|
|
|
bool isBalanced(struct TreeNode* root){
|
|
if (root == NULL)
|
|
return 1;
|
|
int left = height(root->left);
|
|
int right = height(root->right);
|
|
return abs(left - right) <= 1 && isBalanced(root->left) && isBalanced(root->right);
|
|
}
|
|
|