std::cout influence test result


  • 0
    L

    The following cpp code can pass a specific test case

    ...
    TreeNode *now;
    ...
    while (now != NULL) {
    ...
    }
    

    Later I add a std::cout like this:

    ...
    TreeNode *now;
    ...
    while (now != NULL) {
    std::cout << now->val << std::endl;
    ...
    }
    

    Surprisingly, the result, or the structure of the tree changed. It seems that std::cout influenced result, because if I annotate the line, the result will come back to an expected one.
    Maybe something is wrong with pointers causing a memory pollution. Maybe the compiler misunderstood my code while optimizing it. But I can't understand how std::cout did it?


Log in to reply
 

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