[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 ...
随机推荐
- AsMVC:一个简单的MVC框架的Java实现
当初看了<从零开始写一个Java Web框架>,也跟着写了一遍,但当时学艺不精,真正进脑子里的并不是很多,作者将依赖注入框架和MVC框架写在一起也给我造成了不小的困扰.最近刚好看了一遍sp ...
- Java设计模式---装饰模式
装饰模式又名包装(Wrapper)模式.装饰模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式的结构 装饰模式以对客户透明的方式动态地给一个对象附加上更多的责任.换言之,客户 ...
- LSI MegaCl i命令使用1
MegaCli命令使用:cd /opt/MegaRAID/MegaCli/MegaCli -AdpAllInfo -aAll [显示所有适配器信息]MegaCli -LDInfo -Lall ...
- YII框架中php入口文件隐藏
Apache配置修改 主要修改下httpd文件中的两个地方 1.启用mod_rewrite.so模块,在Apache的配置文件中找到如下行,去掉前面的字符"#",保存 #LoadM ...
- phpMyAdmin配置及 错误 缺少 mysqli 扩展。请检查 PHP 配置
PHPMyadmin配置文件config.inc.php内容如下,在需要设置的地方增加了相关注释. 非常适合对数据库操作命令不熟悉的数据库管理者,下面我就说下怎么安装该工具: 1.先到网上下载 ...
- 有关phpmailer的详细介绍及使用方法
第一,需要下载PHPMailer文件包phpmailer. http://phpmailer.sourceforge.net/第二,确认你的服务器系统已经支持socket ,通过phpinfo();查 ...
- HTML5 学习
1.<header> 标签定义文档的页眉(介绍信息) 标签是 HTML 5 中的新标签 <header> <h1>Welcome to my homepage< ...
- Redhat Enterprise 5.4下安装配置Oracle 11g R2详细过程
1.Linux环境配置准备 环境:Linux:Redhat Enterprise 5.4,DB:Oracle 11g R2 X64,Oracle安装到/home/oralce_11目录下. 配置过程如 ...
- ASP.NET执行cmd命令
批处理命令,是执行速度最快效益最高的命令.因为批处理命令,说白了,就是ms-dos环境下的命令,有很多的批处理命令,都是纯DOS下的命令. 然而,批处理命令尽管功能强大,却存在不足之处.批处理命令只能 ...
- 类似百度音乐唱片播放时CD图片不停旋转的实现
类似百度音乐唱片播放时CD图片不停旋转的实现 效果图 1 html代码 2 <imgsrc="img/logo.png"class="img-responsive& ...