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. hdu 2582 f(n) 数学

    打表找规律: 当n为质数是,GCD(n)=n; 当n为质数k的q次方时,GCD(n)=k; 其他情况,GCD(n)=1. 代码如下: #include<iostream> #include ...

  2. hdu 1905 小数化分数2

    ;}

  3. C#中的文件操作

    在.NET Framework 中进行的所有输入和输出工作都要用到流(stream) 有两种类型的流: 输出流:当向某些外部目标写入数据时,就要用到输出流(将数据写入到文件中). 输入流:用于将数据读 ...

  4. linux下用非root用户重启导致ssh无法连接的问题

    问题描述 安装好了centOS服务器,一直用Secure CRT工具通过ssh服务来远程连接linux,很方便的进行各种操作.今天偶然尝试了一下在非root的一般用户下执行重启服务器的命令,发现一般用 ...

  5. lintcode :Remove Duplicates from Sorted Array II 删除排序数组中的重复数字 II

    题目: 删除排序数组中的重复数字 II 跟进“删除重复数字”: 如果可以允许出现两次重复将如何处理? 样例 给出数组A =[1,1,1,2,2,3],你的函数应该返回长度5,此时A=[1,1,2,2, ...

  6. 如何在服务(Service)程序中显示对话框

    原文:http://www.vckbase.com/index.php/wv/94 服务程序(Service)一般是不能和用户进行交互的,所以他一般是不能显示窗口的.要和用户进行交互(如显示窗口),我 ...

  7. login.java

    import java.awt.*; import javax.swing.*; import java.awt.event.*; public class Login extends JFrame ...

  8. Android应用开发学习笔记之Intent

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Intent是什么呢?来看Android官网上的定义: An intent is an abstractdescri ...

  9. python判断文件目录是否存在

    import os os.path.isfile('test.txt')  # 如果不存在就返回False os.path.exists(directory)  # 如果目录不存在就返回False o ...

  10. uva11181Probability|Given

    枚举,条件概率. 2^20次方等于100w,是大约可以没准还能过的. 二进制枚举时,如果买东西的人恰好为r个,设概率为p,就将sum[i]+=p(sum[i]为r个人买东西时第i个人买东西的概率),t ...