题目
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
思路
根据二叉查找树的性质,中序遍历会得到递增有序的排序。所以修改一下中序遍历,找到第k个就终止遍历。
代码
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
struct TreeNode{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x),left(nullptr),right(nullptr){}
};
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
if(k <= 0 || root == nullptr){
return -1;
}
int result = 0;
int index = 0;
InOrder(root,k,index,result);
return result;
}
private:
void InOrder(TreeNode* root,int k,int &index,int &result){
if(root){
if(root->left){
InOrder(root->left,k,index,result);
}
++index;
if(index > k){
return;
}
if(index == k){
result = root->val;
return;
}
if(root->right){
InOrder(root->right,k,index,result);
}
}
}
};
int main(){
Solution s;
int k = 7;
TreeNode *root = new TreeNode(5);
TreeNode *node1 = new TreeNode(2);
TreeNode *node2 = new TreeNode(3);
TreeNode *node3 = new TreeNode(4);
TreeNode *node4 = new TreeNode(9);
TreeNode *node5 = new TreeNode(6);
TreeNode *node6 = new TreeNode(7);
TreeNode *node7 = new TreeNode(11);
root->left = node2;
root->right = node4;
node2->left = node1;
node2->right = node3;
node4->left = node5;
node4->right = node7;
node5->right = node6;
cout<<s.kthSmallest(root,k)<<endl;
return 0;
}
运行时间
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
378.Kth_Smallest_Element_in_a_Sorted_Matrix有序矩阵中第K小的元素【LeetCode单
703.Kth_Largest_Element_in_a_Stream数据流中的第K大元素【LeetCode单题讲解系列】
用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现Leetcode题目.zip用C语言实现...
JVM 基础 JAVA 并发 JVM 性能调优 LeetCode 算法 .......
My Solutions to Leetcode Database problems. 我的 Leetcode 数据.zip
Leetcode 题解.pdf
leetcode 1124 java arts What is ARTS? Algorithm:At least finishing one leetcode problem each week Review:At least review one technical article each week Tip:At least study one technical skill each...
LeetCode 101_C++_算法_leetcode_leetcode101_leetcode101.zip
LeetCode 后端.zip
Leetcode101.zip
原创:leetcode 111. 二叉树的最小深度记住:最小深度和最大深度方法不同。* Definition for a binary tree node.in
Recording personal Java, Python, JavaScript solutions for Leetcode problems. 记录个人 Java, Python, JavaScript 的Leetcode题解.zip
刷leetcode总结.md
原创:leetcode 107. 二叉树的层次遍历 II【队列】* Definition for a binary tree node.
原创:leetcode 5. 最长回文子串//寻找以i-1,i为中点偶数长度的回文//寻找以i为中心的奇数长度的回文。
My Solutions to Leetcode problems. All solutions support C
原创:leetcode 22.括号生成【回溯】对待这种问题,千万别暴力搜索,那样太笨了。但是这个方法是最容易理解的//回溯法 (后面的括号) 不可以大于 (前面
leetcode-editor,在ide中做leetcode练习,支持leetcode.com和leetcode-cn.com,以满足练习的基本需求。理论上支持:intellij idea phpstorm webstorm pycharm rubymine appcode clion goland datagrip rider mps ...