Skip to content

Latest commit

 

History

History
20 lines (19 loc) · 577 Bytes

82.md

File metadata and controls

20 lines (19 loc) · 577 Bytes

Balanced Binary Tree

LeetCode Link

    int Height(TreeNode* root){
        if(!root){
            return 0;
        }
        int leftHeight=Height(root->left);
        int rightHeight=Height(root->right);
        if(leftHeight==-1||rightHeight==-1 || abs(leftHeight-rightHeight)>1){
            return -1;
        }

        return max(leftHeight,rightHeight)+1;
    }
    bool isBalanced(TreeNode* root) {
        if(root==nullptr) return true;
        int height=Height(root);
        return height!=-1;
    }