Kth Smallest Element in a BST LeetCode solution in C++

1
class Solution {
2
public:
3
void findInorder(TreeNode *root, vector<int> &ans) {
4
if (root == NULL) return;
5

6
findInorder(root->left, ans);
7
ans.push_back(root->val);
8
findInorder(root->right, ans);
9
}
10
int kthSmallest(TreeNode *root, int k) {
11
vector<int> ans;
12
findInorder(root, ans);
13
return ans[k - 1];
14
}
15
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0