8ms Accepted C Recursive Solution


  • 0
    C
    
    #include <math.h>
    bool isBalanced(struct TreeNode* root) {
        if (root == NULL) return 1;
        else if (abs(max(root->right) - max(root->left)) > 1 ) return 0;
        else return isBalanced(root->right) && isBalanced(root->left);
    }
    
    int max(struct TreeNode* node) {
        if (node == NULL) return 0;
        int r = 0, l = 0;
        r = max(node->right) +1;
        l = max(node->left) + 1;
        return (r > l) ? r : l;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.