[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.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
问题:找出二叉搜索树种第 k 小的元素。
一个深度遍历的应用。使用递归、或者借助栈都可以实现深度遍历。本文代码使用递归实现。
void visit(TreeNode* node){
if (node->left != NULL){
visit(node->left);
}
if (cnt == ) {
return;
}
cnt--;
if(cnt == ){
res = node->val;
return ;
}
if(node->right != NULL){
visit(node->right);
}
}
int cnt;
int res;
int kthSmallest(TreeNode* root, int k) {
cnt = k;
if(root == NULL){
return ;
}
visit(root);
return (cnt == ) ? res : ;
}
[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 ...
- 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 ...
- 【刷题-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 ...
随机推荐
- yii cgridview 对生成的数据进行分页
这个其实最简单 在对应的model里找Search方法 找到后,参见如下代码 public function search() { // @todo Please modify the followi ...
- nginx 站点80跳443配置
server { listen 80; server_name www.furhacker.cn; location /{# return 301; rewrite ^(.*)$ https://$h ...
- CSS 命名规则
CSS书写顺序: 位置属性(position, top, right, z-index,display, float等) 大小(width, height, padding, margin) 文字系列 ...
- hdu 1869
题意是给m组人物关系,然后判断是否符合六度分离,代码主要就是三个for那里 然后要记得后面判断的时候是大于7,这题除了Florde算法,还有另外一种算法,不过我没记.... #include < ...
- activeMQ总结
队列模式和发布订阅模式的区别 topic只有所有订阅者都消费了,这个消息才会消失.只要有一个订阅者没有消费(持久化模式),这个消息就会存在.订阅者下线然后上线也会读取到这个消息.而且队列的话,消费能力 ...
- (转)js arguments对象
在javascript中,不需要明确指出参数名,就能访问它们.如: function hi(){if(arguments[0]=="andy"){ return;}aler ...
- linux学习笔记<基本知识普及>
linux上分区类型 主分区 : 最多自能有4个 扩展分区 : 最多只能有1个 主分区加扩展分区最多只能有4个 不能写入数据,只能包含逻辑分区 逻辑分区 挂载(安装linux系统时若自定义分区,需注 ...
- Win8节省C盘空间攻略
问题分析: 1.系统页面文件(虚拟内存)占用空间 2.自动更新的缓存文件 3.系统保护的备份文件(系统还原用的) 4.休眠文件 5.索引文件 6.桌面文件 解决办法: 1.机器是8G内存,完全不需要虚 ...
- new date() 函数在浏览器中的兼容问题!!
引言: 同一种语言javascript,在不同的浏览器中,存在语言兼容性问题,本质上是由于不同的浏览器是支持的语言标准和实现上各有差异.本文将基于new Date来创建Date对象来分析这个问题. v ...
- 输入框 input只能输入正数和小数点
输入框 input只能输入正数和小数点 限制文本框只能输入正数,负数,小数 onkeyup="value=value.replace(/[^\-?\d.]/g,'')" 限制文本 ...