Balanced Binary Tree - LeetCode

PHOTO EMBED

Sat May 29 2021 16:43:50 GMT+0000 (UTC)

Saved by @Randumkeng

class Solution {
    int flag;
public:
    int height(TreeNode* root)
    {
        if(root==NULL)return 0;
        int hl = height(root->left);
        int hr = height(root->right);
        if(abs(hl-hr)>1)flag=1;
        return 1+max(hl,hr);
    }
    bool isBalanced(TreeNode* root) {
        
        int h = height(root);
        if(flag)return false;
        else return true;
    }
};
content_copyCOPY

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

https://leetcode.com/problems/balanced-binary-tree/