题意:

  寻找一棵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. JDE报表开发笔记(R5537011 收货校验统计表)

    业务场景:根据批次收货,收货后对该批次产品进行检验,记录检验结果生成统计表. 涉及表:主表F37011,业务从表F43121/F4101/F4108 ------------------------- ...

  2. was7中文redhat6上安装出现中文乱码解决方案

    转:http://blog.csdn.net/w1985g/article/details/8789378 在rhel-server-6.1-x86_64上安装WebSphere 7时,安装界面出现中 ...

  3. linux 一些命令

    1.查看cpu个数 cat /proc/cpuinfo |grep "physical id" |sort | uniq |wc -l 2 2.查看cpu逻辑个数 cat /pro ...

  4. 随机分类器的ROC和Precision-recall曲线

    随机分类器,也就是对于一个分类问题,随机猜测答案.理论上,随机分类器的性能是所有分类器的下界.对随机分类器的理解,可以帮助更好的理解分类器的性能指标.随机分类器的性能也可以作为评价分类器的一个基础.所 ...

  5. System.Web.Optimization找不到引用

    在程序包管理控制程序中录入:Install-Package Microsoft.AspNet.Web.Optimization,安装即可.

  6. mybatis中的mapper.xml

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...

  7. C#在线更新程序[下载程序、解压缩程序、控制台程序]

    [1]下载文件 using System;using System.Collections.Generic;using System.Linq;using System.Text;using Syst ...

  8. SpinEdit

    用code给value赋值会触发 change事件

  9. javaNIO是什么?由那几部分组成?各部分的作用。

    Java NIO 由以下几个核心部分组成: Channels Buffers Selectors 虽然Java NIO 中除此之外还有很多类和组件,但在我看来,Channel,Buffer 和 Sel ...

  10. java运算符的优先级

    Java 编辑 运算符 结合性 [ ] . ( ) (方法调用) 从左向右 ! ~ ++ -- +(一元运算) -(一元运算)  从右向左 * / % 从左向右 + - 从左向右 << & ...