二叉平衡树的判断,使用递归的方法
class Solution {
public:
bool isBalanced(TreeNode* root) {
return findDepth(root) == -1 ? false : true;
}
int findDepth(TreeNode* root)
{
if(!root) return 0;
int leftDepth = findDepth(root->left);
if(leftDepth == -1) return -1;
int rightDepth = findDepth(root->right);
if(rightDepth == -1) return -1;
if(abs(leftDepth-rightDepth) > 1) return -1;
return max(leftDepth, rightDepth)+1;
}
};