问题描述: Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 用了两个递归,一个递归来判断是否是平衡树,一个递归用来求左右子树的高度。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 int getHeight(struct TreeNode*root){
     if(root == 0) return 0;
     int L,R;
     L = getHeight(root->left);
     R = getHeight(root->right);
     if(L<R) return R + 1;
     else return L + 1;
 }

bool isBalanced(struct TreeNode* root) {
    if (root == NULL) return true;
    struct TreeNode*left = root->left;
    struct TreeNode*right = root->right;
    int diff;
    diff = getHeight(left) - getHeight(right);
    diff = abs(diff);
    if(diff>1) return false;
    return isBalanced(left)&&isBalanced(right);
}