/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int gethigh(TreeNode* node){
if (node == NULL) return 0;
int lhigh = 1 + gethigh(node->left);
int rhigh = 1 + gethigh(node->right);
int high = max(lhigh, rhigh);
return high;
}
bool isBalanced(TreeNode* root) {
if (root == NULL) return true;
int lhigh = gethigh(root->left);
int rhigh = gethigh(root->right);
if (abs(lhigh - rhigh) > 1 ) return false;
return isBalanced(root->left) && isBalanced(root->right);//每一个左右子树都要平衡
}
};