Is this a bug of OJ?


  • 0
    W

    https://leetcode.com/problems/convert-bst-to-greater-tree/description/
    In the problem above, i have my solution like this:

    class Solution {
    public:
    	TreeNode* convertBST(TreeNode* root) {
    		static int last_sum = 0;
    		if (root)
    		{
    			convertBST(root->right);
    			root->val += last_sum;
    			last_sum = root->val;
    			convertBST(root->left);
    		}
    		return root;
    	}
    };
    

    This snippet failed to pass the testcase on the OJ but ran well on my Visiual Studio 2013 with the same testcase.
    If i use a member variable instead of the static variable in that member function like:

    class Solution {
        int last_sum = 0;
    public:
        TreeNode* convertBST(TreeNode* root) {
            if(root)
            {
                convertBST(root->right);
                root->val+=last_sum;
                last_sum = root->val;
                convertBST(root->left);
            }
            return root;
        }
    };
    

    it works well and got Accepted.
    Can someone tell me why this happens? Is this a bug of OJ? Many thanks.


Log in to reply
 

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