平衡二叉树

class Solution {
public:
    int depth(TreeNode* pRoot)
        {
        if(pRoot==NULL)
            return 0;
        if(pRoot->left==NULL&&pRoot->right==NULL)
            return 1;
        else return depth(pRoot->left)>depth(pRoot->right)?depth(pRoot->left)+1:depth(pRoot->right)+1;
    }
    bool IsBalanced_Solution(TreeNode* pRoot) {
          if(pRoot==NULL)
              return true;
        if(pRoot->left==NULL&&pRoot->right==NULL)
            return true;
       if(IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right))
            {
            int left=depth(pRoot->left);
            int right=depth(pRoot->right);
            if(left<=right+1&&left>=right-1)
                return true;
            else return false;
        }
        else return false;
    }
};
原文地址:https://www.cnblogs.com/daocaorenblog/p/5400876.html