C++ | Balanced Binary Tree | using DFS


#1
int dfs(TreeNode *A, bool &flag) {
    if(not A)               return 0;
    if(not flag)            return -1;
    int l = dfs(A->left, flag);
    int r = dfs(A->right, flag);
    if(abs(l - r) > 1)      flag = false;
    return max(l, r)+1;
}
int Solution::isBalanced(TreeNode* A) {
    bool flag = true;
    int d = dfs(A, flag);
    return flag;
}