For the same example,in "run",return the correct output, "submit",return wrong,why??


  • 0
    R

    here is my code, as for the example [0,1], when I try to "run", it returns "1"(which is the right answer), while I tried to submit, it returned "100",I don't know why?

    class Solution {
    public:
        int sumNumbers(TreeNode* root) {
      static int out = 0;
      if (root == NULL) return out;
      stack<pair<TreeNode*, int>> store;
      store.push(make_pair(root, out));
      while (!store.empty()) {
        pair<TreeNode*, int>node = store.top();
        store.pop();
        int value = node.second;
        if (node.first->left == NULL && node.first->right == NULL)
          out += value + node.first->val;
        if (node.first->right != NULL) {
          value = value * 10 + node.first->val;
          store.push(make_pair(node.first->right, value));
        }
        if (node.first->left != NULL) {
          value = value * 10 + node.first->val;
          store.push(make_pair(node.first->left, value));
        }
      }
      return out;  
        }
    };
    

  • 0

    @remina You are using static variable.

    Please see this:
    https://leetcode.com/faq/#different-output


Log in to reply
 

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