C solution, 8ms - clean but EVIL...


  • 0
    J

    But I want more evil...

    #include <setjmp.h>
    
    static char jmpLoc[256];
    
    int MaxChildDepth(struct TreeNode * node) {
        int l, r;
        
        if (node == NULL) {
            return 0;
        }
        l = MaxChildDepth(node->left);
        r = MaxChildDepth(node->right);
        if (l - r < -1 || l - r > 1) {
            longjmp(jmpLoc, 1);
        }
        return l < r ? r + 1 : l + 1;
    }
    
    bool isBalanced(struct TreeNode* root) {
        if (setjmp(jmpLoc) == 0) {
            MaxChildDepth(root);
            return true;
        }
        return false;
    }

Log in to reply
 

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