leetCode(46):Kth Smallest Element in a BST
Given a binary search tree, write a function kthSmallest to find the kth
smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
直观地一想。查找第k小的数,不就是遍历到第k个数吗?所以中序遍历非常easy想到,例如以下:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> nodeStack;
TreeNode* tmp=root;
int kthMin = 0;
int kthValue;
while ((!nodeStack.empty() || tmp!=NULL) && kthMin!=k)
{
while (tmp != NULL)
{
nodeStack.push(tmp);
tmp = tmp->left;
}
tmp = nodeStack.top();
nodeStack.pop();
kthMin++;//对中序遍历稍加改动
kthValue = tmp->val;
tmp = tmp->right;
}
return kthValue;
}
};
另外。看了一下网友的解答,很巧妙。
他是先统计左子树上节点个数,假设节点个数小于k。则在右子树上找第k-n-1小的数,假设刚为k则就是当前节点,假设大于k,则继续在左子树上找第k小的数。
只是。每次递归都要统计一次节点个数,会不会导致复杂度添加?
int kthSmallest(TreeNode* root, int k) {
if (!root) return 0;
if (k==0) return root->val;
int n=count_size(root->left);
if (k==n+1) return root->val;
if (n>=k){
return kthSmallest(root->left, k);
}
if (n<k){
return kthSmallest(root->right, k-n-1);
}
}
int count_size(TreeNode* root){
if (!root) return 0;
return 1+count_size(root->left)+count_size(root->right);
}
leetCode(46):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
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小的元素
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 ...
- Java for LeetCode 230 Kth Smallest Element in a BST
解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { ...
- 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. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...
随机推荐
- logstash tcp multihost output(多目标主机输出,保证TCP输出链路的稳定性)
在清洗日志时,有一个应用场景,就是TCP输出时,须要在一个主机挂了的情况下,自已切换到下一个可用入口.而原tcp output仅支持单个目标主机设定.故本人在原tcp的基础上,开发出tcp_multi ...
- hdoj--2098--分拆素数和(枚举)
分拆素数和 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- FFmpeg 移植 Android
近期项目需要解析苹果的HLS流媒体协议,而FFmpeg从0.11.1“Happiness”版本开始,才增加了对HLS协议的支持.目前网上关于FFmpeg编译移植的文章有很多,但大多都是对旧版本的说明. ...
- Ubuntu 16.04或14.04里下安装搜狗输入法(图文详解)(全网最简单)
不多说,直接上干货! 其实啊,很简单 分三步走 1.添加fcitx的键盘输入法系统,因为sogou是基于fcitx的,而系统默认的是iBus: 2.安装sogou输入法: 3.设置系统参数及一些注意点 ...
- Android GreenDao 使用教程
上一篇 总结了grendao 环境搭建以及简单的增删查改,接下来将全面解析框架的使用,基于上篇的orm模型(Note)数据库讲解 GreenDao的插入: 插入的方式有很多: daoSession.g ...
- CommandType.Text
CommandType.Text代表执行的是SQL语句CommandType.StoreProcedure代表执行的是存储过程CommandType代表要执行的类型 //返回DataTable的SQL ...
- offscreen-render
offscreen-render 什么是offscreen-render?offscreen-render涉及的内容比较多,有offscreen-render那就有onscreen render,on ...
- RunLoop主要处理以下6类事件
1.Observer事件,runloop中状态变化时进行通知.(微信卡顿监控就是利用这个事件通知来记录下最近一次main runloop活动时间,在另一个check线程中用定时器检测当前时间距离最后一 ...
- Pyhton学习——Day60
Form组件 - form表单(验证:保留上次内容) - - Ajax(验证:无需上次内容) - 返回HttpResponse - 前端:跳转或错误信息 1. Form组件扩展: 1.简单扩展 利用F ...
- epoll的边缘触发与水平触发
epoll的边缘触发与水平触发 Tcp连接是双向的,内核为每个socket维护两个缓冲区,读缓冲区与写缓冲区,内核会一个关注这两个缓冲区,当采用水平触发时,对于写缓冲区而言,如果有多余空间可写,对于读 ...