LeetCode Kth Smallest Element in a BST(数据结构)
题意:
寻找一棵BST中的第k小的数。
思路:
递归比较方便。
/**
* 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 countNode(TreeNode* root)//计算子树有多少个节点,其实还可以将这个函数直接融到下面的函数中,用K来标记功能即可。
{
if(root==NULL) return ;
int L=countNode(root->left);
int R=countNode(root->right);
return L+R+;
}
int kthSmallest(TreeNode* root, int k)
{
int cnt=countNode(root->left);
if(cnt+==k) return root->val;
if(cnt>=k) kthSmallest(root->left,k);
else kthSmallest(root->right,k-cnt-);
}
};
AC代码
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
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. 二叉搜索树中第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- ...
随机推荐
- CSS 框模型——规定了元素框处理元素内容、内边距、边框和外边距的方式
转自:http://www.w3school.com.cn/css/css_boxmodel.asp 要知道在父元素:float, rel, abs位置情况下,box模型的变换情况,请见:http:/ ...
- return false取消手机移动端的默认设置
想做一个语音界面,当长按语音按钮的时候,总会出现移动端什么复制粘贴菜单.然后在JS中加入return false后就消失了,感觉好神奇哦~
- PDF 补丁丁 0.4.1 版将增加嵌入中文字库的功能
有不少用户反映,部分老 PDF 文件由于在制作时没有嵌入字库,导致该文件在某些阅读器上显示为乱码.即使他们用 Acrobat 嵌入了相应的字库,文件仍然无法正确显示. 这些老 PDF 看起来具有如下相 ...
- Octopus系列之关于多选属性如何在OO中表示呢?
在电子商务系统中 关于产品属性的问题 会设计如下几个表 产品信息 Product 选项信息表 Option 存储 Size Color.... 选项值信息表 O ...
- C#获取指定日期为一年中的第几周
/// <summary> /// 获取指定日期,在为一年中为第几周 /// </summary> /// <param name="dt">指 ...
- 可滑动的ToggleButton(开关)
2013-12-28 17:25:01 网上看到一篇关于可滑动的ToogleButton的文章,有代码,觉得挺好,但是不符合我的要求,因此在他的代码基础上改了一些.(作者看到了勿喷啊,实在找不到原文了 ...
- C++-dynamic_cast的用处
主要用来在没有实现文件,只有头文件的情况下,添加派生类的功能,如下例给programmer加奖金. 注意:dynamic_cast不能用于没有virtual函数的类 ///////////////// ...
- [vijos P1014] 旅行商简化版
昨天早上上课讲旅行商问题,有点难,这周抽空把3^n的算法码码看.不过这个简化版已经够折腾人了. 其一不看解析不知道这是双进程动态规划,不过我看的解析停留在f[i,j]表示第一个人走到i.第二个人走到j ...
- 解决python version 2.7 required,which was not find in the registry
程序自动写注册表 http://www.vvivv.com/post-143.html 手工写 http://blog.csdn.net/baikaishui525/article/details/9 ...
- c#读写文本文档-1-用file类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...