首页 > 其他 > 详细

leetcode-剑指55-II

时间:2021-01-25 09:08:29      阅读:26      评论:0      收藏:0      [点我收藏+]

同主站110
address

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

// 左子树是,右子树是,左剪右-1,0,1,
// 搞个函数,判断,是的话就返回高度,不是的话就-1
bool isBalanced(struct TreeNode* root){
    int judge(struct TreeNode* tree){
        if(!tree)
            return 0;
        int left = judge(tree->left);
        int right = judge(tree->right);
        if((left<0)||(right<0))
            return -1;
        int cha = left-right;
        if((cha<-1)||(cha>1))
            return -1;
        if (right>left)
            left = right;
        return (left+1);
    }
    int ok = judge(root);
    if(ok<0)
        return false;
    return true;
}

leetcode-剑指55-II

原文:https://www.cnblogs.com/gallien/p/14323159.html

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