LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)
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个最小的元素,中序遍历就可以了,具体代码和另一个Binary Tree Iterator差不多其实,这题由于把=写成了==调bug调了好久,细心细心啊啊啊。代码如下:
/**
* 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 *> s;
map<TreeNode *, bool> m;
if(root == NULL) return ;
s.push(root);
while(!s.empty()){
TreeNode * t = s.top();
if(t->left && !m[t->left]){
s.push(t->left);
m[t->left] = true;
continue;
}
s.pop(); //这里pop
k--;
if(k == )
return t->val;
if(t->right && !m[t->right]){
s.push(t->right);
m[t->right] = true;
} }
}
};
java 版本的如下所示,同儿茶搜索树迭代器实际上是一样的:
public class Solution {
public int kthSmallest(TreeNode root, int k) {
int res;
HashMap<TreeNode, Integer> map = new HashMap<TreeNode, Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
if(root == null)
return 0;
stack.push(root);
while(!stack.isEmpty()){
TreeNode node = stack.peek();
while(node.left != null && !map.containsKey(node.left)){
stack.push(node.left);
map.put(node.left, 1);
node = node.left;
}
if(--k == 0)
return node.val;
stack.pop(); //这一步不要忘了
if(node.right != null && !map.containsKey(node.right)){
stack.push(node.right);
map.put(node.right, 1);
}
}
return 0;
}
}
LeetCode OJ:Kth Smallest Element in a BST(二叉树中第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小的元素
题目大意 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 ...
- (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 ...
- 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. Not ...
- [LeetCode] 215. Kth Largest Element in an Array 数组中第k大的数字
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 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第一刷_Rotate Image
这个题该怎么说呢.旋转又要求inplace.一般就是要找到某种规律了.这个还是非常明显的,画一下原来的.再画一下旋转之后的.看看原来的跑到什么位置了. 牵扯到四个位置按顺时针方向互换一下位置,发现仅仅 ...
- studio显示Surface: getSlotFromBufferLocked: unknown buffer: 0xa2a58be0
根据查询外网资料来看,出现这个错误的原因大致是换个模拟器或者物理机就可以了. 因为我使用的是安卓6.0,貌似都会出现这类的问题. 但是不影响程序运行.
- 0607am抽象类&接口&析构方法&tostring&小知识点
/*class ren{ public static $color;//静态 static function () { ren::$color; self::$color;//self只能写在类里面, ...
- 从yum源下载软件包
以下是从163源下载openstack-ocata版软件包的脚本: from html.parser import HTMLParser from urllib import request impo ...
- vue(组件、路由)懒加载
const Login = resolve => require(['@/components/Login'], resolve) //就不用import了 Vue.use(Router) le ...
- Linux Shell编程 条件判断语法
if条件判断语句 单分支 if 条件语句 语法格式: if [条件判断式];then 程序 fi 或者 if [条件判断式] then 程序 fi 在使用单分支 if 条件查询时需要注意几点: if ...
- 【HackerRank】Median
题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大 ...
- 5.1深入理解计算机系统——系统级I/O
一.UNIX I/O 在UNIX系统中有一个说法,一切皆文件.所有的I/O设备,如网络.磁盘都被模型化为文件,而所有的输入和输出都被当做对相应文件的读和写来执行.这种将设备映射为文件的方式,允 ...
- EXP-00008:遇到ORACLE错误904问题
案例情景--在一次Oracle 数据库导出时: C:\Documents and Settings\Administrator>exp lsxy/lsxy@lsxy_db file=E:\lsx ...
- pom.xml里使用了一系列的版本的框架,配置一个版本属性,让使用版本的都引用这个属性
在pom.xml定义properties标签 <properties> <project.build.sourceEncoding>UTF-8</project.buil ...