剑指 Offer 55 - II. 平衡二叉树 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root==nullptr) return true;
if(abs(fun1(root->left)-fun1(root->right))<=1)
return (isBalanced(root->left)&&isBalanced(root->right));
return false;
}
int fun1(TreeNode*root){
if(root==nullptr) return 0;
int l=fun1(root->left);
int r=fun1(root->right);
return max(l,r)+1;
}
};