解题思路:

直接修改中序遍历函数即可,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的更多相关文章

  1. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  2. [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 ...

  3. 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 ...

  4. (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 ...

  5. [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 ...

  6. LeetCode 230 Kth Smallest Element in a BST 二叉搜索树中的第K个元素

    1.非递归解法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  7. LeetCode 230. Kth Smallest Element in a BST 动态演示

    返回排序二叉树第K小的数 还是用先序遍历,记录index和K进行比较 class Solution { public: void helper(TreeNode* node, int& idx ...

  8. 【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 ...

  9. 【刷题-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 ...

随机推荐

  1. fatal: Paths with -a does not make sense.

    git commit -am '*屏蔽设置缓存' htdocs/s.php fatal: Paths with -a does not make sense. 应该用下面的这样. git commit ...

  2. zoj3811 Untrusted Patrol (dfs)

    2014牡丹江网络赛C题 (第三水的题 The 2014 ACM-ICPC Asia Mudanjiang Regional First Round http://acm.zju.edu.cn/onl ...

  3. Sql导出数据报错-->SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问,因为此组件已作为此服 ...

  4. 遍历一个类的属性--并转换为Dictionary类型

    参考地址...http://www.cnblogs.com/xwgli/p/3306297.html 记录点滴...以前很少用泛型...HaHa... /// <summary> /// ...

  5. 【C语言入门教程】4.7 指针的地址分配 - mallocl(), free()

    指针变量可指向任何类型的变量,在处理过程中,指针变量指向的变量通过传递变量的地址来实现,指针变量的取值是内存的地址,这个地址应当是安全的,不可以是随意的.否则,写入内存单元的值将使得已存放的数据或程序 ...

  6. maven 工程启动找不到 Spring ContextLoaderListener 的解决办法

    1.错误:  Error configuring application listener of class org.springframework.web.context.ContextLoader ...

  7. QQ空间个人中心的广告

    http://qzonestyle.gtimg.cn/qzone/space_item/boss_pic/*.jpghttp://img*.paipaiimg.com/*.jpghttp://cn.q ...

  8. iOS开发——网络篇——文件下载(NSMutableData、NSFileHandle、NSOutputStream)和上传、压缩和解压(三方框架ZipArchive),请求头和请求体格式,断点续传Range

    一.小文件下载 NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion ...

  9. 字串符相关 split() 字串符分隔 substring() 提取字符串 substr()提取指定数目的字符 parseInt() 函数可解析一个字符串,并返回一个整数。

    split() 方法将字符串分割为字符串数组,并返回此数组. stringObject.split(separator,limit) 我们将按照不同的方式来分割字符串: 使用指定符号分割字符串,代码如 ...

  10. jquery版悬浮模块demo

    在做在线客服时,代码就是按照该模块命名.现在,我要添加一个返回主页的功能,我觉得再复制一遍之前的代码,那个量有点多,如果我再添加一个功能,那个量会很多……现在我用创建对象字面量的方式来创建(其实我还想 ...