What's the difference between "NULL" and "null" in LeetCode?


  • 0
    G

    If the input is [1,2,2,3,NULL,NULL,3,4,NULL,NULL,4],the answer is true;
    but if the input is [1,2,2,3,null,null,3,4,null,null,4],the answer becomes false.
    Why the same input provides different results?


  • 1

    "Why the same input provides different results?"

    Well they're not the same input.

    And you can try printing the tree to see that the "NULL" version confuses the parser:

    bool isBalanced(TreeNode* root) {
        queue<TreeNode*> q;
        q.push(root);
        while (q.size()) {
            auto node = q.front();
            q.pop();
            if (node) {
                cout << node->val << ",";
                q.push(node->left);
                q.push(node->right);
            } else {
                cout << "null,";
            }
        }
        return true;
    }
    

    For [1,2,2,3,null,null,3,4,null,null,4] this prints:

    1,2,2,3,null,null,3,4,null,null,4,null,null,null,null,

    For [1,2,2,3,NULL,NULL,3,4,NULL,NULL,4] this prints:

    1,2,2,3,0,0,3,4,0,0,4,null,null,null,null,null,null,null,null,null,null,null,null,


Log in to reply
 

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