(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 ...
随机推荐
- LINUX常用配置及命令
一. Fedora系统配置 1. [设置网卡IP] 步骤如下: 1) 用root用户登陆,打开/etc/sysconfig/network-scripts/ifcfg-eth0文 ...
- 客户端TortoiseSVN的安装及使用方法
一.客户端TortoiseSVN的安装 运行TortoiseSVN程序,点击Next,下面的截图顺序即为安装步骤: 图1: 图2: 图3: 图4: 点击Finish按钮后会提示重启系统,其实不重启也没 ...
- PhoneGap 在 Android 上的插件开发方法介绍
移动应用开发已经成为软件开发的一个重要方向,但是移动开发面临的一个重要问题就是跨平台的问题.PhoneGap 作为一个多平台的软件开发框架,提供了一次编写多个平台的运行.目前已经支持多达 6 个移动平 ...
- Entity Framework菜鸟初飞
Entity Framework菜鸟初飞 http://blog.csdn.net/zezhi821/article/details/7235134
- erlang远程加载模块须知
erlang加载本地beam到远程节点,需要把依赖库一个个手动加载,否则他不会自动加载. 另外,创建lib的话,使用 rebar-creator create-lib
- 异常处理:Sys.WebForms.PageRequestManagerParserErrorException:The message……
如果你为了使页面可以达到局部刷新的效果,并且用了UpdatePanel控件,这是如果你在后台页面用到Response对象时肯呢过会抛出一下异常: 解决方法:$(document).ready(func ...
- Oracle-单表合并列
表基本结构 合并列 select t.student,decode(t.java,'','','java') 科目, t.java from student t union select t.stud ...
- hibernate级联与反向
cascade:设置本表与关联表之间的级联操作,如:设置为save-update,则插入或更新对象时同时保存或更新另一端的表,但不会产生关联关系数据,除非inverse为false. inverse: ...
- ADF_General JSF系列1_创建一个简单的JSF Application
2015-02-17 Creatd By BaoXinjian
- PLSQL_基础系列01_正则表达REGEXP_LIKE / SUBSTR / INSTR / REPLACE(案例)
2014-11-30 Created By BaoXinjian