110. Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

S: recursion

    bool isBalanced(TreeNode* root) {
        return getDepth(root) >= 0;
    }

    int getDepth(TreeNode* root) {
        if(!root) return 0;
        int left = getDepth(root->left);
        if(left < 0) return -1;
        int right = getDepth(root->right);
        if(right < 0 || abs(left-right) > 1) return -1;
        return max(left, right)+1;
    }

results matching ""

    No results matching ""