平衡二叉树的每一棵子树都是平衡二叉树
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(!root)
return true;
return abs(height(root->left) - height(root->right))<= 1 && isBalanced(root->right) && isBalanced(root->left);
}
int height(TreeNode* root){
if(!root)
return 0;
return max(height(root->right),height(root->left))+1;
}
};