Help: Same solution accepted in leetcode for same problem

interview-questions
Tags: #<Tag:0x00007f24251edf20>

#1

The same question is in leetcode and the below solution is accepted there. Here it is showing weird errors.

/**

  • Definition for binary tree
  • struct TreeNode {
  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    /
    vector v;
    void inorder(TreeNode
    ptr){
    if(ptr!=NULL){
    inorder(ptr->left);
    v.push_back(ptr->val);
    inorder(ptr->right);
    }
    }

int Solution::kthsmallest(TreeNode* A, int B) {
inorder(A);
if(B-1 > v.size())
return -1;
return v[B-1];

}


#2

You have used a global vector v. Pass it as an argument instead of making it global. leetcode allows global variables, but over here the global values are carried forward to the next test case.


#3

It worked , thank you very much