Java for LeetCode 230 Kth Smallest Element in a BST
解题思路:
直接修改中序遍历函数即可,JAVA实现如下:
int res = 0;
int k = 0; public int kthSmallest(TreeNode root, int k) {
this.k = k;
inorderTraversal(root);
return res;
} public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
if (root == null)
return list;
if (root.left != null && list.size() < k)
list.addAll(inorderTraversal(root.left));
list.add(root.val);
if (root.right != null && list.size() < k)
list.addAll(inorderTraversal(root.right));
if(list.size()==k){
res=list.get(k-1);
return list;
}
return list;
}
Java for 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 ...
- (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. 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 二叉搜索树中的第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 ...
随机推荐
- iOS-开启arc之后 NSNotificationCenter removeObserver 是否需要调用
开启ARC之后,NSNotificationCenter removeObserver 是否需要调用,在何时调用? 今天在stackoverflow上面看到一个问题,arc情况下是否需要调用remov ...
- GOF业务场景的设计模式-----单例模式
个人觉得 纯粹的学习设计模式,是不对的.也不能为了使用设计模式,而硬搬设计模式来使用 单例模式可能是 最简单的设计模式也是 大家知道最多的设计模式.当然 ,有很多种写法 定义:确保一个类只有一个实例, ...
- edwin报警和监控平台开源了(python源码)
简单介绍一下edwin edwin是一个报警和监控平台, 可以使用它监控任意东西, 如有异常(分为警告级和严重级), 可以发出报警. 可以自定义报警的通知方式, 比如邮件/短信/电话. 另外, 它提供 ...
- ELK日志分析系统搭建(转)
摘要: 前段时间研究的Log4j+Kafka中,有人建议把Kafka收集到的日志存放于ES(ElasticSearch,一款基于Apache Lucene的开源分布式搜索引擎)中便于查找和分析,在研究 ...
- 摄像头/光驱故障:由于其配置信息(注册表中的)不完整或已损坏,Windows 无法启动这个硬件设备。 (代码 19)
原因:出现这条提示一般都是因为注册表错误引起的,重装驱动或应用程序无法解决. 设备管理器中相关设备-故障如图: 以下方法可以解决问题: 1.在任务栏上点“开始”菜单-“运行”,输入”regedit“ ...
- iOS: Crash文件解析(一)
iOS Crash文件的解析(一) 开发程序的过程中不管我们已经如何小心,总是会在不经意间遇到程序闪退.脑补一下当你在一群人面前自信的拿着你的App做功能预演的时候,流畅的操作被无情地Crash打断. ...
- solrcloud
@Test public void querySolrCloud(){ String zkHost = "127.0.0.1:2181"; String defaultCollec ...
- jersey
http://www.cnblogs.com/bluesfeng/archive/2010/10/28/1863816.html
- js实现自定义右键菜单--兼容IE、Firefox、Chrome
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- iOS开发——UI进阶篇(九)block的巧用
前面有提到通知.代理.kvo等方法来协助不同对象之间的消息通信,今天再介绍一下用block来解决这个问题 接着前面的例子 这里将功能在复述一遍 我把用block和通知放在一起比较,当然代理和kvo如何 ...