/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int>obj;
vector<int> inorderTraversal(TreeNode* root) {
if(root==NULL)
{
return obj;
}
if(root->left!=NULL)
{
inorderTraversal(root->left);
}
obj.push_back(root->val);
if(root->right!=NULL)
{
inorderTraversal(root->right);
}
return obj;
}
int kthSmallest(TreeNode* root, int k) {
obj=inorderTraversal(root);
return obj[k-1];
}
};
LeetCode:230. 二叉搜索树中第K小的元素
最新推荐文章于 2024-10-23 19:56:25 发布