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 ...
随机推荐
- 响应式Web初级入门
本文来自我的前端博客,原文地址:http://www.hacke2.cn/about-responsive/ 跨终端时代的到来 当你乘坐各种交通工具(公交.地铁.轻轨.火车)时你会发现,人们都个个低下 ...
- log4net--不可多得的开源日志记录组件
log4net--不可多得的开源日志记录组件 1 前奏 一直在用log4net日志工具,却没时间写个日志给大家分享一下这个工具,趁最近比较空些,好好分享一下这个工具. 2 说明 Log4net介绍就不 ...
- [译]Mongoose指南 - 验证
开始前记住下面几点 Validation定义在SchemaType中 Validation是一个内部的中间件 当document要save前会发生验证 验证不会发生在空值上 除非对应的字段加上了 re ...
- 图解Tomcat类加载机制
说到本篇的tomcat类加载机制,不得不说翻译学习tomcat的初衷. 之前实习的时候学习javaMelody的源码,但是它是一个Maven的项目,与我们自己的web项目整合后无法直接断点调试.后来同 ...
- 1_mysql +DBA职业发展
MYSQL + DBA 职业发展 mysql :the world's most popular open source database 最流行的开源数据库 数据库世界 关系数据库(又称SQL数据库 ...
- Swift2.1 语法指南——析构过程
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- Linux中获取本机网络信息的几个函数及应用
一.读取/etc/hosts 几个函数 头文件<netdb.h> 1.void sethostent(int stayopen);//开打/etc/hosts 配置文件 2.struct ...
- 常用的Git Tips
导读 Git被越来越多的公司使用,因此我们需要了解Git使用过程中的一些技巧. 一.Configuration:配置 列举所有的别名与配置 git config --list Git 别名配置 git ...
- am335x sd卡启动开启识别emmc kernel 上的改动
sbc 7109-454 sd 卡启动qt系统后一直识别不了 emmc 也就是mmc1口, 一开始以为是硬件初始化的问题,后面又以为是io口复用,最后才知道是根本没有注册mmc1设备. 更改下面的代 ...
- java计算时间差
比如:现在是2016-03-26 13:31:40 过去是:2016-01-02 11:30:24 我现在要获得两个日期差,差的形式为:XX天XX小时XX分XX秒 方法一: DateFo ...