码迷,mamicode.com
首页 > 其他好文 > 详细

剑指Offer - 平衡二叉树

时间:2018-02-14 11:45:18      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:www.   title   code   item   color   subject   pos   desc   treenode   

https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=13&tqId=11192&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

 

题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。
 
 

代码

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

 

 

技术分享图片

 

剑指Offer - 平衡二叉树

标签:www.   title   code   item   color   subject   pos   desc   treenode   

原文地址:https://www.cnblogs.com/charlesblc/p/8447965.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!