int node_height(struct TreeNode* root, bool* balance)
{
if (root == NULL)
{
return -1;
}
int left_height = node_height(root->left, balance);
int right_height = node_height(root->right, balance);
if (left_height - right_height > 1 || right_height - left_height > 1)
{
*balance = false;
}
return fmax(left_height, right_height) + 1;
}
bool isBalanced(struct TreeNode* root)
{
if (root == NULL)
{
return true;
}
if (root->left == NULL && root->right == NULL)
{
return true;
}
bool balance = true;
node_height(root, &balance);
return balance;
}
110. 平衡二叉树
最新推荐文章于 2024-10-08 10:36:00 发布