牛客 判断是否是平衡二叉树

class Solution {
public:
    bool IsBalanced_Solution(TreeNode* pRoot) {
              if(NULL==pRoot)
                   return true; 
        if(pRoot->right==NULL&&pRoot->left==NULL)
            return true;
          int left,right;
            if(pRoot->left!=NULL)
             {
              left=Deep(pRoot->left);
              IsBalanced_Solution(pRoot->left);}
            else
              left=0;
             if(pRoot->right!=NULL)
             {
               right=Deep(pRoot->right);
               IsBalanced_Solution(pRoot->right);
             }
            else
               right=0;
              if(left-right>1||left-right<-1)
                      return false;
          
         
              return true;
    }
    int Deep(TreeNode* pRoot)
        {
          if(NULL==pRoot)
              return 0;
           int left=Deep(pRoot->left);
           int right=Deep(pRoot->right);
              return 1+(left>right? left:right);
    }
};

posted @ 2017-03-30 00:43  爱编程的小羊  阅读(228)  评论(0编辑  收藏  举报