(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 ...
随机推荐
- 一步一步理解Paxos算法
一步一步理解Paxos算法 背景 Paxos 算法是Lamport于1990年提出的一种基于消息传递的一致性算法.由于算法难以理解起初并没有引起人们的重视,使Lamport在八年后重新发表到 TOCS ...
- Android: 在 TextView 里使用删除线
Android: 在 TextView 里使用删除线 分类: Android2014-09-25 13:17 3431人阅读 评论(0) 收藏 举报 以编程的方式添给 TextView 添加删除线: ...
- httpClenit的post出现乱码问题
在使用httpClient.executeMethod(postMethod)的时候,发现一直存在乱码问题,”book is good“被转成”book+is+good“ 返回.查看源码后,发现pos ...
- 在64位系统使用PLSQL Developer
由于PLSQL Developer没有提供64位的,于是根据网上的资料做了一下整理,发上来 1.下载并安装Oracle 11g R2 64位,在服务器上安装时忽略硬件检测失败信息: 2.下载Oracl ...
- ubuntu设置环境变量
sudo gedit /etc/environment path结尾处追加 路径,如::/opt/EmbedSky/4.3.3/bin source /etc/environment,或者重启电脑?? ...
- [转]java生成随机数字和字母组合
摘自 http://blog.csdn.net/xiayaxin/article/details/5355851 import java.util.Random; public String getC ...
- Oracle维护常用SQL
--查询表空间.表空间大小及表空间对应物理路径 select a.tablespace_name,b.file_name,a.block_size,a.block_size,b.bytes/1024 ...
- zend studio 13 curl 请求本机地址 无法跟踪调试的问题解决方案。。。(chrome等浏览器调试原理相同)
方案如下: <?php $ch = curl_init (); curl_setopt ($ch, CURLOPT_URL, 'http://YOUR-SITE.com/your-script. ...
- (转)LitJson 遍历key
本文转载自:http://blog.csdn.net/inlet511/article/details/47127579 用LitJson插件获取到的对象,如果想遍历对象中包含的子对象的key,可以用 ...
- bzoj3905: Square
Description Nothing is more beautiful than square! So, given a grid of cells, each cell being black ...