【刷题-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
1to10^4. - You may assume
kis 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 ...
随机推荐
- 使用openssl生成证书,并通过Nginx配置
创建服务器证书密钥文件 server.key openssl genrsa -des3 -out server.key 2048 这个时候会提示输入密码 这个密码要记住 openssl语法 opens ...
- github访问慢处理办法
Windows 系统:C:\Windows\System32\drivers\etc\hostsLinux 系统:/etc/hostsMac(苹果电脑)系统:/etc/hostsAndroid(安卓) ...
- JS将时间戳转换为日期格式
function getDate(time){ var date =(new Date(parseInt(time))).toLocaleDateString() return date; } tim ...
- windows10下vscode+cmake编译Qt5代码
概述 本文演示环境: windows10 cmake version:3.18 qt version: 5.14 vscode version: 1.54.1 本文演示环境基于在线模式, 离线使用VS ...
- Codeforces 913C:Party Lemonade(贪心)
C. Party Lemonade A New Year party is not a New Year party without lemonade! As usual, you are expec ...
- Go package(3):io包介绍和使用
IO 操作的基本分类 在计算机中,处理文件和网络通讯等,都需要进行 IO 操作,IO 即是 input/ouput,计算机的输入输出操作. Go语言中的 IO 操作封装在如下几个包中: io 为 IO ...
- 设置div背景透明的两种方法
实现div背景透明的两种方法 1.使用opacity属性 background-color:#000; opacity: 0.5; 这样做可以设置div内部所以区域的透明度,但是也会影响里面的文字,效 ...
- uniapp以及微信小程序中scroll-view隐藏滚动条 自定义滚动条
隐藏滚动条 1.全局隐藏滚动条,在app.vue中 ::-webkit-scrollbar{ display: none; } 2.局部隐藏藏滚动条 样式没有使用scoped属性时, 否则无效. .u ...
- WEB文档在线预览解决方案
web页面无法支持预览office文档,但是却可以预览PDF.flash文档,所以大多数解决方案都是在服务端将office文档转换为pdf,然后再通过js的pdf预览插件(谷歌浏览器等已经原生支持嵌入 ...
- 低成本CH7511芯片方案|CH7511电路设计参考|CS5211替代CH7511
CH7511是主要用于设计eDP转LVDS转换器,怎么样设计一款低成本低BOM简单的DP转LVDS的转接设置,目前有一款可以替代兼容CH7511的方案电路,并且其设计电路整体BOM成本较低,并且设计简 ...