Using inorder traversal of a tree ( Self explanatory )


#1

vector inOrder(TreeNode* root) {
vector ans;
if(root==NULL) {
return ans;
}

vector<int> leftAns=inOrder(root->left);
for(int i=0;i<leftAns.size();i++) {
    ans.push_back(leftAns[i]);
}

ans.push_back(root->val);

vector<int> rightAns=inOrder(root->right);
for(int i=0;i<rightAns.size();i++) {
    ans.push_back(rightAns[i]);
}

return ans;

}

int Solution::kthsmallest(TreeNode* A, int B) {

vector<int> inorder=inOrder(A);
return inorder[B-1];

}