LeetCode 230. Kth Smallest Element in a BST
2016-02-24 16:04
274 查看
class Solution { public: int rank; int result; void help(TreeNode* root, int k){ if(!root) return; help(root->left, k); if(++rank == k){ result = root->val; return; } help(root->right, k); } int kthSmallest(TreeNode* root, int k) { rank = 0; help(root, k); return result; } };
rank即为当前root在整棵树中的顺序,表示当前root->val为第rank小的数(是指++rank后的rank)。
还有用stack实现的算法,未细看,留个坑。
hint里的未优化,留坑。
相关文章推荐
- Android自定义控件之仿美团下拉刷新
- ROS下使用科大讯飞SDK进行在线语音识别
- ubuntu error: the HTTP rewrite module requires the PCRE library.
- UITabBarController--选项卡栏控制器
- concurrentHashMap 到底采取了什么措施使得它比synchronized(HashMap)好
- 常见的几种RuntimeException
- [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III
- CSS——对列表应用样式和创建导航栏
- Android Theme.AppCompat.Light报错
- 裁剪图片之个人总结
- 调试Android WebView
- sublime text3 常用插件
- 查看 SELinux状态及关闭SELinux
- java多线程学习--java.util.concurrent
- SynchronizedMap和ConcurrentHashMap的深入分析
- 进一步封装highchart,打造自己的图表插件:jHighChart.js
- 泛型(二)
- 对象
- jquery.cookie.js 1.4.1
- 面试题201-225