110. Balanced Binary Tree easy题
这题有偷看别人怎么写
我好烂
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(!root) return true;
bool balanced = true;
height(root, &balanced);
return balanced;
}
private:
int height(TreeNode* root, bool* balanced){
if(!root) return 0;
int left_height = height(root->left, balanced);
int right_height = height(root->right, balanced);
if(abs(left_height - right_height)>1) {
*balanced = false;
return -1;
}
return max(left_height, right_height) + 1;
}
};