110.Balanced Binary Tree

Posted smallredness

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了110.Balanced Binary Tree相关的知识,希望对你有一定的参考价值。

class Solution {
public:    
    bool isBalanced(TreeNode *root) {
        if (checkDepth(root) == -1) return false;
        else return true;
    }
    int checkDepth(TreeNode *root) {
        if (!root) return 0;
        int left = checkDepth(root->left);
        if (left == -1) return -1;
        int right = checkDepth(root->right);
        if (right == -1) return -1;
        int diff = abs(left - right);
        if (diff > 1) return -1;
        else return 1 + max(left, right);
    }
};

以上是关于110.Balanced Binary Tree的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode[110]-Balanced Binary Tree

LC.110. Balanced Binary Tree

110. Balanced Binary Tree

110. Balanced Binary Tree

110. Balanced Binary Tree

110. Balanced Binary Tree 110.平衡二叉树