Leetcode 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.
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?
Hint:
- Try to utilize the property of a BST.
- What if you could modify the BST node's structure?
- The optimal runtime complexity is O(height of BST).
题目意思:
给定一棵二叉搜索树,找到第k小的元素
注意:
1、利用二叉搜索树的特性
2、修改二叉搜索树的节点结构
3、最优时间复杂度是0(树的高度)
解题思路:
方法一:
二叉搜索树的特性:其中序遍历是有序的。故中序遍历访问,访问第k个元素即可。
方法二:
利用分冶的方法。
- 统计根节点左子树的节点个数cnt
- 如果cnt+1 = k,则根节点即为第k个最小元素
- 如果cnt+1 > k,则第k个最小元素在左子树,root = root->left;
- 如果cnt+1 < k,则第k个最小元素在右子树,root = root->right;
- 重复第一步
源代码:
方法一:
class Solution {
public:
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> nodeStack;
if(root == NULL) return -;
while(true){
while(root){
nodeStack.push(root);
root = root->left;
}
TreeNode* node = nodeStack.top(); nodeStack.pop();
if(k == ) return node->val;
else root = node->right,k--;
}
}
};
方法二:
class Solution {
public:
int calcNodeSize(TreeNode* root){
if( root == NULL) return ;
return + calcNodeSize(root->left) + calcNodeSize(root->right);
} int kthSmallest(TreeNode* root, int k) {
if(root == NULL) return ;
int cnt = calcNodeSize(root->left);
if(k == cnt + ) return root->val;
else if( k < cnt + ) return kthSmallest(root->left,k);
else return kthSmallest(root->right, k-cnt-);
}
};
Leetcode Kth Smallest Element in a BST的更多相关文章
- [LeetCode] 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 Kth Smallest Element in a BST(数据结构)
题意: 寻找一棵BST中的第k小的数. 思路: 递归比较方便. /** * Definition for a binary tree node. * struct TreeNode { * int v ...
- [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. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...
- 【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 ...
- 【刷题-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 k ...
- 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 ...
- 【LeetCode】230. Kth Smallest Element in a BST
Difficulty: Medium More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...
随机推荐
- Apache commons-configuration setDelimiterParsingDisable不生效的处理
Apache commons-configuration setDelimiterParsingDisable不生效的处理 项目中有用到commons-configuration,版本1.9. 配置初 ...
- php简单框架的应用实例
<html> <frameset rows="50%,50%"> <frame src="/Test/header.php"> ...
- Jni :三维数组处理方法 ,以整形三维数组为例 C++实现
本文原创,转载请注明地址:http://www.cnblogs.com/baokang/p/4982846.html 关于Jni的基本使用方法,请参阅:Java 调用 C++ (Java 调用 dll ...
- BZOJ 3639: Query on a tree VII
Description 一棵树,支持三种操作,修改点权,修改颜色,问所有与他路径上颜色相同的点的最大权,包含这两个点. Sol LCT. 用LCT来维护重边,对于每个节点在建一个set用来维护轻边,这 ...
- Struts开发包结构
- signalR selfhost 版本兼容问题
一.异常简要说明 最近在学习signalR,i按照http://www.asp.net/signalr/overview/deployment/tutorial-signalr-self-host 这 ...
- js_原型
原型是JavaScript中一个比较难理解的概念,原型相关的属性也比较多,对象有"prototype"属性,函数对象有"prototype"属性,原型对象有&q ...
- mysql workbench连接不上远程数据库,xshell无法连接远程主机的问题
1.先说xshell无法连接的问题 最近使用virtualbox装了个ubuntu-16.04,然后在win7上使用xshell连接,首先确认win7能ping通虚拟机ip.然后确认是否安装了open ...
- Shell 脚本实现随机抽取班级学生
#/bin/bash function rand(){ min=$ max=$(($-$min+)) num=$(date +%s%N) echo $(($num%$max+$min)) } rnd= ...
- BFS_Maze_求解迷宫最短路径
/* 10 10 #.######.# ......#..# .#.##.##.# .#........ ##.##.#### ....#....# .#######.# ....#..... .## ...