(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 ...
随机推荐
- DataRow[] 转为数组
DataRow[] rows = dt.Select("1=1"); ].ToString()).ToArray();
- RMAN备份与恢复之不完全恢复
要点:对于RMAN的不完全恢复,有如下步骤: 1)加载数据到mount状态(建议恢复前先做备份) 2)为高并发分配多个通道 3)还原所有(所需)的数据文件 4)使用until time,until s ...
- DW(六):polybase访问Azure Blob Storage
目录: 连接hadoop配置语法 配置hadoop连接 Pushdown配置 Create external tables for Azure blob storage 连接hadoop配置语法: g ...
- php日期时间函数
1,年-月-日echo date('Y-m-j');2007-02-6echo date('y-n-j');07-2-6大写Y表示年四位数字,而小写y表示年的两位数字:小写m表示月份的数字(带前导), ...
- 【python】浅谈enumerate 函数
enumerate 函数用于遍历序列中的元素以及它们的坐标: >>> for i,j in enumerate(('a','b','c')): print i,j 0 a 1 b ...
- [platform]新旧内核的device设备注册对比
转自:http://blog.chinaunix.net/uid-7332782-id-3268801.html 1. Version2.6内核启动过程 start_kernel( ) //板子上电启 ...
- 【MySQL】数据行长度的一些限制
今天开发在导入数据的时候报一个错误: Row size too large. The maximum row size for the used table type, not counting BL ...
- 杀死future处理的阻塞线程
public class Test{ public static void main(String[] args){ ExecutorService pool = Executors.newFixed ...
- HtmlParser + HttpClient 实现爬虫
简易爬虫的实现 HttpClient 提供了便利的 HTTP 协议访问,使得我们可以很容易的得到某个网页的源码并保存在本地:HtmlParser 提供了如此简便灵巧的类库,可以从网页中便捷的提取出指向 ...
- PLSQL_PLSQL调优健康检查脚本SQLHC(案例)
2014-08-23 Created By BaoXinjian