(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 ...
随机推荐
- JAVA线程池ThreadPoolExecutor-转
首先是构造函数签名如下: public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeU ...
- 【jmeter】浅说 think time
接口每天被5000个人调用,同时在线500人,每天要被调用50000次. 过了没多久测试完成写了一份报告发给项目经理: 并发 | 响应时间 | 应用服务器cpu |数据库服务器cpu |TPS | ...
- Python基础教程【读书笔记】 - 2016/6/26
希望通过博客园持续的更新,分享和记录Python基础知识到高级应用的点点滴滴! 第一波:第6章 抽象 [总览] 介绍函数.参数parameter.作用于scope概念,以及递归概念. [6.1] 函 ...
- bzojj1764: [Baltic2009]monument
Description 给一个p*q*r的立方体,它由p*q*r个1*1*1的小立方体构成.每个立方体要么被虫蛀,要么不被.现在郑爽要选出一个a*a*b的立方体(方向任意),使得它没有被虫蛀过,并且4 ...
- define宏定义中的#,##,@#及\符号
define宏定义中的#,##,@#及\符号 在#define中,标准只定义了#和##两种操作.#用来把参数转换成字符串,##则用来连接两个前后两个参数,把它们变成一个字符串. 1.# (string ...
- [tty与uart]3.tty驱动分析
转自:http://www.wowotech.net/linux_kenrel/183.html 目录: 1 首先分析设备驱动的注册 1.1 uart_register_driver分析 1.2 tt ...
- 黄聪:PHP5.6+7代码性能加速-开启Zend OPcache-优化CPU
说明 PHP 5.5+版本以上的,可以使用PHP自带的opcache开启性能加速(默认是关闭的).对于PHP 5.5以下版本的,需要使用APC加速,这里不说明,可以自行上网搜索PHP APC加速的方法 ...
- (C/C++) memset
C语言: memset extern void *memset(void *buffer,int c,int count); #include <string.h> 功能:把b ...
- C# STUDY
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- RMAN_学习实验2_RMAN Duplicate复制数据库过程(案例)
待整理 对于基于生产环境下的数据库的版本升级或者测试新的应用程序的性能及其影响,备份恢复等等,我们可以采取从生产环境以克隆的方式将其克隆到本地而不影响生产数据库的正常使用.实现这个功能我们可以借助rm ...