leetcode-剑指55-II

同主站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;
}
posted @ 2021-01-25 00:55  RougeBW  阅读(29)  评论(0编辑  收藏  举报