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小的结点

思路:

  只要明白BST树的原理,只要中序遍历一遍BST树即可。求第K小的,只需遍历前K个结点就OK。

C++:

 /**
* 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 ret, cnt, _k; void rec(TreeNode *root)
{
if(root->left == && root->right == )
{
cnt++;
if(cnt == _k)
ret = root->val;
return ;
} if(root->left != )
rec(root->left); cnt++;
if(cnt == _k){
ret = root->val;
return ;
} if(root->right != )
rec(root->right);
} int kthSmallest(TreeNode* root, int k) {
if(root == )
return ; _k = k; cnt = ret = ; rec(root); return ret;
}
};

Python:

 # Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution:
# @param {TreeNode} root
# @param {integer} k
# @return {integer} def __init__(self):
self.cnt = 0
self.ret = 0 def rec(self, root, k):
if root.left is None and root.right is None:
self.cnt = self.cnt + 1
if self.cnt == k:
self.ret = root.val
return if root.left is not None:
self.rec(root.left, k) self.cnt = self.cnt + 1
if self.cnt == k:
self.ret = root.val
return if root.right is not None:
self.rec(root.right, k) def kthSmallest(self, root, k):
if root is None:
return 0 self.rec(root, k) return self.ret

【LeetCode 230】Kth Smallest Element in a BST的更多相关文章

  1. 【树】Kth Smallest Element in a BST(递归)

    题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...

  2. LeetCode OJ: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 ...

  3. 【LeetCode 215】Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

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

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

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

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

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

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

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

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

  9. 【LeetCode】230. 二叉搜索树中第K小的元素 Kth Smallest Element in a BST

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:算法题,刷题,Leetcode, 力扣,二叉搜索树,BST ...

随机推荐

  1. .NET复习笔记

    .NET 基础知识点汇总 课前知识储备. 一.C#与.NET的区别? 1..NET/dotnet:一般指.Net Framework框架,一种平台,一种技术 2.C#(sharp):一种编程语言,可以 ...

  2. BZOJ 3129 SDOI2013 方程

    如果没有限制,答案直接用隔板法C(m-1,n-1) 对于>=x的限制,我们直接在对应位置先放上x-1即可,即m=m-(x-1) 对于<=x的限制,由于限制很小我们可以利用容斥原理将它转化为 ...

  3. 写个Python练练手吧

    在Python的交互式命令行写程序,好处是一下就能得到结果,坏处是没法保存,下次还想运行的时候,还得再敲一遍. 所以,实际开发的时候,我们总是使用一个文本编辑器来写代码,写完了,保存为一个.py文件, ...

  4. NSMutableArray 初始化与添加删除程序

           Person *person1=[[Person alloc]initWithName:@"Kenshin"];        Person *person2=[[P ...

  5. 基于条件随机场(CRF)的命名实体识别

    很久前做过一个命名实体识别的模块,现在有时间,记录一下. 一.要识别的对象 人名.地名.机构名 二.主要方法 1.使用CRF模型进行识别(识别对象都是最基础的序列,所以使用了好评率较高的序列识别算法C ...

  6. 分享一个免费SSL证书申请网站,给网站开启https协议 | 张戈博客

    这些天,由于公司的业务需求,接触到了ssl证书和https协议.博客前几篇文章也分享了在WEB服务器上安装SSL证书,为网站开启https协议的教程,感兴趣的童鞋可以前往查看相关文章: <Lin ...

  7. nyoj-291 互素数个数 欧拉函数

    LK的数学题 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 LK最近遇到一个问题,需要你帮她一下.一个整数n,求[1,n)中,和n互素的数的个数.   输入 多组测 ...

  8. NFC(4)响应NFC设备时启动activity的四重过滤机制

    响应NFC设备时启动activity的四重过滤机制 在一个NFC设备读取NFC标签或另一个NFC设备中的数据之前会在0.1秒之内建立NFC连接,然后数据会自动从被读取一端流向读取数据的一端(NFC设备 ...

  9. 方法Equals和操作符==的区别

    http://www.codeproject.com/Articles/584128/What-is-the-difference-between-equalsequals-and-Eq When w ...

  10. 删除 GPT 保护分区

    问题: 将内置和/或外置硬盘连接到 Windows XP 32 位操作系统时,将无法访问硬盘,“磁盘管理”将会报告该硬盘包含 GPT 保护分区.在此状态下,将无法对硬盘进行重新分区和格式化. 原因: ...