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

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);
    }
};

    原文作者:爱编程的小羊
    原文地址: https://www.cnblogs.com/cxy931980808/p/6642900.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞