Middle-题目16:230. Kth Smallest Element in a BST
来源:互联网 发布:淘宝内部优惠券哪个好 编辑:程序博客网 时间:2024/05/24 04:45
题目原文:
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.
题目大意:
求一个BST的第k小的节点。
题目分析:
根据二叉排序树的性质,中序遍历序列第k个元素即为所求。
源码:(language:java)
public class Solution { public int kthSmallest(TreeNode root, int k) { List<Integer> list=new ArrayList<Integer>(); traverse(root,list); return list.get(k-1); } public void traverse(TreeNode node,List<Integer> list) { if(node!=null) { traverse(node.left,list); list.add(node.val); traverse(node.right,list); } return; }}
成绩:
2ms,beats 16.50%,众数1ms,49.50%
0 0
- Middle-题目16:230. Kth Smallest Element in a BST
- 230.Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- 230. Kth Smallest Element in a BST
- Merge k Sorted Lists(hard)
- 【MyBatis】——mybatis缓存以及整合ehcache
- 1006. Sign In and Sign Out (25)
- 解决微信开发onclick事件不执行的问题
- android解析PDF、XPS文件的第三方类库muPdf
- Middle-题目16:230. Kth Smallest Element in a BST
- Unity3D学习日记(九) EasyMovieTexture视频播放插件使用浅谈
- linux常用基本命令之二十六 cut
- MFC编程之控件的禁用与激活
- 有关进度条 JProgressBar的应用
- JAVA中对存储过程的调用方法
- Middle-题目17:116. Populating Next Right Pointers in Each Node
- 在内部存储写文件
- HDU 5355 (构造 dfs剪枝)