【刷题-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 kth smallest element in it.
Example 1:
Input: root = [3,1,4,null,2], k = 1
3
/ \
1 4
\
2
Output: 1
Example 2:
Input: root = [5,3,6,2,4,null,null,1], k = 3
5
/ \
3 6
/ \
2 4
/
1
Output: 3
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
Constraints:
- The number of elements of the BST is between
1
to10^4
. - You may assume
k
is always valid,1 ≤ k ≤ BST's total elements
.
解法1 利用BST中序序列有序的特点找第k次访问的节点
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
int order = 0, ans = INT_MAX;
bool flag = false;
in_order(root, order, k, flag, ans);
return ans;
}
void in_order(TreeNode* root, int &order, int k, bool &flag, int &ans){
if(flag)return;
if(!root)return;
in_order(root->left, order, k, flag, ans);
order++;
if(order == k){
flag = true;
ans = root->val;
}
in_order(root->right, order, k, flag, ans);
}
};
解法2 将BST修改成为中序的线索二叉树
【刷题-LeetCode】230. 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 二叉搜索树中的第K小的元素
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 ...
- (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. 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 ...
随机推荐
- Vue3.0是如何变快的
1.diff算法优化 + Vue2中的虚拟dom是进行全量的对比 https://vue-next-template-explorer.netlify.app/ + Vue3新增了静态标记(Patch ...
- 深度解析HashMap
讲讲HashMap? 源码解析 final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { //辅助 ...
- JavaScript扫盲笔记:
JavaScript学习: JavaScript代码存在形式: -Head中 <script> </script> -文件 <script src='代码所在路径'> ...
- Shell脚本--数值比较组合判断
用于数值比较的无非大于.小于.等于.大于等于.小于等于这几个. 比较格式: [ 数值1 比较符 数值2 ] 注意左边的括号与数值1之间有一个空格,同样,数值2和右边的括号之间也有空格. 数值比较运 ...
- Java高级:条件队列与同步器Synchronizer的原理+AQS的应用
14.构建自定义的同步工具 类库中包含了许多存在状态依赖性的类,例如FutureTask,Semaphore和BlockingQueue等.在这些类中的一些操作中有着基于状态的前提条件,例如,不能从一 ...
- 使用xlsx实现Excel导入
需求 实现在系统里批量导入数据,通过上传一个excel文件,前端将文件处理为json数据发送给后端.(最好与后端定义好上传的文件模板,方便处理数据) 实现 使用xlsx: xlsx的github地址: ...
- centos下修改hosts文件以及生效命令
修改 vim /etc/hosts 生效 service network restart 或者 /etc/init.d/network restart
- Linux(Centos)内存占用过高处理
查看内存占用最大 ps aux| grep -v "USER" |sort -n -r -k 4 |awk 'NR==1{ print $0}' 命令查看占用内存最大的10个进程 ...
- Windows库函数获取 可执行程序所在路径
头文件 #include <Windows.h> 函数 函数已经写好了 std::string get_executable_dir_() { char path[255] = { 0 } ...
- 【LeetCode】143. Reorder List 解题报告(Python)
[LeetCode]143. Reorder List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...