C++ easy solution using inorder traversal


#1
  1. void inorder(TreeNode *curr,vector<int> &ans){
  2. if(curr==NULL)
  3. return;
  4. else{
  5. if(curr->left!=NULL)
  6. inorder(curr->left,ans);
  7. ans.push_back(curr->val);
  8. if(curr->right!=NULL)
  9. inorder(curr->right,ans);
  10. }
  11. }
  12. int Solution::kthsmallest(TreeNode* A, int B) {
  13. vector<int> ans;
  14. TreeNode *curr=A;
  15. inorder(curr,ans);
  16. return ans[B-1];
  17. }