判断是否为平衡二叉树。
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root == NULL) return 1;
int l1 = getDepth(root->left);
int l2 = getDepth(root->right);
if(abs(l1-l2)>1) return 0;
return isBalanced(root->left)&&isBalanced(root->right);
}
int getDepth(TreeNode* root){
if(root == NULL) return 0;
return max(getDepth(root->left), getDepth(root->right))+1;
}
};