首页 > 其他 > 详细

平衡二叉树

时间:2016-04-17 14:36:38      阅读:120      评论:0      收藏:0      [点我收藏+]

 

 

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

 

平衡二叉树

原文:http://www.cnblogs.com/daocaorenblog/p/5400876.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!