题意:

  寻找一棵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(数据结构)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素

    题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...

  4. LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)

    230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...

  5. 【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 ...

  6. 【刷题-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 ...

  7. 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 ...

  8. [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 ...

  9. 【LeetCode】230. Kth Smallest Element in a BST

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/kth-smallest- ...

随机推荐

  1. Octopus系列之接下来的任务

    更新默认国家[已实现] 更新每页显示条数的后台控制[已实现] 更新国家和区域的Ajax的关联[已实现] 更新详情页面的 属性选择 脚本提示[已实现 可以做到和兰亭一样的效果了] 增加优惠方案的设置和批 ...

  2. R中rJava包载入时报错的问题

    今天安装XLConnect包,安装后无法library(XLConnect)载入,看报错问题应该出在rJava上,找到了下面的解决办法: if (Sys.getenv("JAVA_HOME& ...

  3. 目前几款基于html5的前端框架:如Bootstrap、Foundation、Semantic UI 、Amaze UI

    Bootstrap是由Twitter在2011年8月推出的开源WEB前端框架,集合CSS 和HTML,使用了最新的浏览器技术,为快速WEB开发提供了一套前端工具包,包括布局.网格.表格.按钮.表单.导 ...

  4. "QQ尾巴病毒"核心技术的实现原理分析

    声明:本文旨在探讨技术,请读者不要使用文章中的方法进行任何破坏. 2003这一年里,QQ尾巴病毒可以算是风光了一阵子.它利用IE的邮件头漏洞在QQ上疯狂传播.中毒者在给别人发信息时,病毒会自动在信息文 ...

  5. PDF创建及动态转换控件程序包ActivePDF Portfolio

    ActivePDF Portfolio是将4个activePDF最优秀的服务器产品捆绑成一个价格适中的控件程序包.它提供了开发一个完整的服务器端的PDF解决方案所需的一切. 具体功能: activeP ...

  6. android textview 跑马灯

    <TextView android:layout_width="match_parent" android:layout_height="48dp" an ...

  7. 【转】CentOS yum安装和卸载软件的使用方法

    在CentOS yum安装和卸载软件的使用方法安装方法安装一个软件时.   CentOS yum -y install httpd安装多个相类似的软件时   CentOS yum -y install ...

  8. RPI学习--环境搭建_无线网络的连接

    这里不讨论无线网卡的驱动问题,假设内核已经支持了该网卡. 发现命令行下添加无线网络比较麻烦,于是利用图形桌面的工具(WiFi Config)先配置好,在回到字符终端,发现网络已经配置好了 查看内网ip ...

  9. SharePoint 2013 重复的管理账户错误:已添加项。字典中的关键字 所添加的关键字

    博客地址:http://blog.csdn.net/FoxDave 今天在管理中心创建新的Web应用程序时,想注册一个新的管理账户,一着急点了两次按钮,结果就出现了这样的错误...怎么说呢,太奇葩 ...

  10. 【python练习】截取网页里最新的新闻

    需求: 在下面这个网页,抓取最新的新闻,按天划分. http://blog.eastmoney.com/13102551638/bloglist_0_1.html 实现方法1:使用递归 import ...