(medium)LeetCode 230.Kth Smallest Element in a BST
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?
代码1:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int kthSmallest(TreeNode root, int k) {
int count =countNodes(root.left);
if(k<=count){
return kthSmallest(root.left,k);
}else if(k>count+1){
return kthSmallest(root.right,k-1-count);
}
return root.val;
}
public int countNodes(TreeNode n){
if(n==null) return 0;
return 1+countNodes(n.left)+countNodes(n.right);
} }
运行结果:

代码2:中序遍历递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private static int number=0;
private static int count=0;
public int kthSmallest(TreeNode root, int k) {
count=k;
helper(root);
return number;
}
public void helper(TreeNode n){
if(n.left!=null) helper(n.left);
count--;
if(count==0){
number=n.val;
return;
}
if(n.right!=null) helper(n.right);
} }
运行结果:
代码3:中序遍历迭代
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution { public int kthSmallest(TreeNode root, int k) {
Stack<TreeNode>st=new Stack<>();
while(root!=null){
st.push(root);
root=root.left;
}
while(k!=0){
TreeNode n=st.pop();
k--;
if(k==0) return n.val;
TreeNode right=n.right;
while(right!=null){
st.push(right);
right=right.left;
}
}
return -1;
} }
运行结果:

代码4:使用队列,中序遍历,存储起来,然后出队n个元素即可。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution { private Queue<TreeNode> queue=new LinkedList<TreeNode>();
public int kthSmallest(TreeNode root, int k) {
inOrder(root);
TreeNode ret=null;
while(k>0){
ret=queue.poll();
k--;
}
return ret.val;
}
public void inOrder(TreeNode root){
if(root==null) return;
if(root.left!=null) inOrder(root.left);
queue.offer(root);
if(root.right!=null) inOrder(root.right); } }
运行结果:

(medium)LeetCode 230.Kth Smallest Element in a BST的更多相关文章
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- [LeetCode] 230. Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- Leetcode 230. Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [LeetCode] 230. Kth Smallest Element in a BST 解题思路
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- Java for LeetCode 230 Kth Smallest Element in a BST
解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...
- LeetCode 230 Kth Smallest Element in a BST 二叉搜索树中的第K个元素
1.非递归解法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- LeetCode 230. Kth Smallest Element in a BST 动态演示
返回排序二叉树第K小的数 还是用先序遍历,记录index和K进行比较 class Solution { public: void helper(TreeNode* node, int& idx ...
- 【LeetCode】230. Kth Smallest Element in a BST (2 solutions)
Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...
- 【刷题-LeetCode】230. Kth Smallest Element in a BST
Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the k ...
随机推荐
- 转载: scikit-learn学习之回归分析
版权声明:<—— 本文为作者呕心沥血打造,若要转载,请注明出处@http://blog.csdn.net/gamer_gyt <—— 目录(?)[+] ================== ...
- 如何计算oracle数据库内存
数据库内存设置: 项目 数据关系 单位 系统CPU n 个 物理内存Memory 假设4G物理内存 4*1024 MB memory_target 0.5*4*1024 0.5*Memory sga_ ...
- 使用CAJViewer 提取PDF文件中的文字
使用 CAJViewer 7.2 软件,把pdf格式的文件提取出文字. 操作步骤参考:http://jingyan.baidu.com/article/d45ad148cd06e469552b800f ...
- window.location.search
http://i.cnblogs.com/EditPosts.aspx?opt=1&opt2=x 就拿上面这个URL来说window.location.search的返回值为opt=1& ...
- (zt)Lua的多任务机制——协程(coroutine)
原帖:http://blog.csdn.net/soloist/article/details/329381 并发是现实世界的本质特征,而聪明的计算机科学家用来模拟并发的技术手段便是多任务机制.大致上 ...
- shopex用户登陆错误提示在nginx下乱码问题
http://www.test.cn/passport-aHR0cDosLHd3dy54eTAwNy5jbixwYXNzcG9ydC1hSFIwY0Rvc0xIZDNkeTU0ZVRBd055NWpi ...
- 并行计算之OpenMP中的任务调度
本文参考<OpenMP中的任务调度>博文,主要讲的是OpenMP中的schedule子句用法. 一.应用需求 在OpenMP并行计算中,任务调度主要用于并行的for循环.当for循环中每次 ...
- [原]对Linux环境下任务调度一点认识
我一直以来有一个误解,那就是在终端运行某个程序时,按下Ctrl + D时我误以为就是杀死了这个进程,今天才知道原来不是.比如我利用libevent在Linux环境下写了一个网络监听程序,当启动程序之后 ...
- http://www.dayandeng.com/ 诈骗网站
http://www.dayandeng.com/ 诈骗网站 http://www.dayandeng.com/userfiles/media/2018/awzosv16.html 骗取你的京 ...
- TCP segment of a reassembled PDU
Wireshark有时候会显示这个东东. 此处PDU是指上层(如HTTP)的Protocol Data Unit,意指上层协议的一个协议段太长,无法放入单个TCP数据包. 如果你在wireshark中 ...