Solution using vector (not recommended)


#1
int Solution::kthsmallest(TreeNode* A, int B) {
vector<int> sol;
stack<TreeNode*> s;
s.push(A);
while(!s.empty()){
    TreeNode* temp=s.top();
    s.pop();
    sol.push_back(temp->val);
    
    if(temp->left!=NULL)
    s.push(temp->left);
    if(temp->right!=NULL)
    s.push(temp->right);
}

sort(sol.begin(),sol.end());
return sol[B-1];

}