LeetCode 230. Kth Smallest Element in a BST
2016-04-22 02:53
501 查看
Given a binary search tree, write a function
kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Just read the question carefully, then, there is no problem. BST.....
kthSmallestto find the
kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Just read the question carefully, then, there is no problem. BST.....
void inorder(TreeNode* root, int &res, int &idx,int k){ if (root==NULL || idx>=k) return; inorder(root->left,res,idx,k); idx++; if (idx ==k ) res = root->val; inorder(root->right,res,idx,k); } int kthSmallest(TreeNode* root, int k) { int res; int idx=0; inorder(root,res,idx,k); return res; }
相关文章推荐
- Objective-C 单例模式
- 项目中遇到的bug及相关经验
- Reduction to two dimensions and Phase plane analysis
- Android开发本地及网络Mp3音乐播放器(五)实现专辑封面图片
- Android开发本地及网络Mp3音乐播放器(五)实现专辑封面图片
- 51Nod-1085-背包问题
- LeetCode 314. Binary Tree Vertical Order Traversal(二叉树垂直遍历)
- [Perl]REAPER
- android性能优化练习:过度绘制
- LeetCode 204. Count Primes
- HDOJ(HDU) 1799 循环多少次?(另类杨辉三角)
- HDOJ(HDU) 1799 循环多少次?(另类杨辉三角)
- Power of Four leetcode 342
- java中的反射
- Unity Shaders and Effects Cookbook (4-3)遮罩反射
- python一些包
- angularjs的post请求参数的转换,使之跟ajax一样参数供springMVC使用
- 网络编程基础知识
- eclipse里访问tomcat首页出现404错误解决之法
- VM虚拟机的复制和安装windows时分区