the result of "run code" with "submit" is different


  • 0
    C

    My code is bellow, but the result of "run code"(res = 5) with "submit"(res = 4, which is wrong) is different, using sample [3,1,null,null,2]. Do you met this?

    Thx

    map<TreeNode*, int> add2val = {{0, 0}};
    
    class Solution {
    public:
        int rob(TreeNode* root) {
            if(root)
            {
                int valLeft = rob(root -> left);
                int valRight = rob(root -> right);
                
                int valRoot = max(valLeft + valRight, root -> val 
                + (root -> left ? add2val[root -> left -> left] : 0) 
                + (root -> left ? add2val[root -> left -> right] : 0)
                + (root -> right ? add2val[root -> right -> left] : 0)
                + (root -> right ? add2val[root -> right -> right] : 0));
                
                add2val.insert(pair<TreeNode*, int>(root, valRoot));
                
                return valRoot;
            }
            
            return 0;
        }
    };
    

Log in to reply
 

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