1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > LeetCode 230二叉搜索树中第k小的元素

LeetCode 230二叉搜索树中第k小的元素

时间:2018-08-11 12:40:00

相关推荐

LeetCode 230二叉搜索树中第k小的元素

思路:

※一、中序遍历,找第k个元素

class Solution {public:vector<int>res;void inorder(TreeNode *root){if(root==NULL){return;}inorder(root->left);res.push_back(root->val);inorder(root->right);}int kth(int k){return res[k-1];}int kthSmallest(TreeNode* root, int k) {inorder(root);return kth(k);}};

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。