http://oj.leetcode.com/problems/balanced-binary-tree/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int isBalancedDepth(TreeNode *root){
if(root==NULL) return 0;
int left=isBalancedDepth(root->left);
int right=isBalancedDepth(root->right);
if(left<0||right<0) return -1;
if(abs(left-right)>1) return -1;
return max(left,right)+1;
}
bool isBalanced(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
return isBalancedDepth(root)>=0;
}
};