Balanced Binary Tree

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.

 

Code:

 

class Solution {
public:
    
    int findBalance(TreeNode *node){
        if(!node) return 0;
        return 1+max(findBalance(node->left),findBalance(node->right));
    }
    
    bool isBalanced(TreeNode *root) {
        if(!root) return true;
        return abs(findBalance(root->left)-findBalance(root->right))<=1&&isBalanced(root->left)&&isBalanced(root->right);
    }
};

 

posted @ 2013-11-19 04:29  WinsCoder  阅读(146)  评论(0编辑  收藏  举报