zoukankan      html  css  js  c++  java
  • 判断二叉树是否平衡

    //                      1
    //              2             3
    //        4                         5
    //
    //
    
    class Solution {
    public:
        int maxDepth(TreeNode* root){
            if(root==NULL)
                return 0;
     
            return 1+max(maxDepth(root->left),maxDepth(root->right));
        }
        bool danbian(TreeNode *root)
            {
            if(root==NULL)
                return 1;
            if(root->left!=NULL && root->right!=NULL)
                return 0;
            if(root->left==NULL)
                ;//danbian(root->right);
            if(root->right==NULL)
                ;//danbian(root->left);
            return 1;
        }
        int minDepth(TreeNode* root){
            
            if(root==NULL)
                return 0;
    
            
    
             if(root->left == NULL) return minDepth(root->right) + 1;
             if(root->right == NULL) return minDepth(root->left) + 1;
    
            int leftDepth = minDepth(root->left);
            int rightDepth = minDepth(root->right);
            return leftDepth < rightDepth ? (leftDepth + 1) : (rightDepth + 1);
        }
        bool IsBalanced_Solution(TreeNode* pRoot) {
            int max=maxDepth(pRoot);
            int min=minDepth(pRoot);
            if(danbian(pRoot))
                min=1;
            if(max-min>1)
                return 0;
            return 1;
            
            
        }
    };
    

      

  • 相关阅读:
    Hdu3022 Sum of Digits
    bzoj3864 Hero meet devil
    bzoj2448 挖油
    poj3783 Balls
    bzoj3802 Vocabulary
    Hdu5181 numbers
    Hdu5693 D Game
    图形填充之边标志算法
    图形填充之栅栏填充算法
    图形填充之种子填充算法
  • 原文地址:https://www.cnblogs.com/laiqun/p/5894071.html
Copyright © 2011-2022 走看看