题目:

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.

思路:

1、计算左子树元素个数leftSize。

2、 leftSize+1 = K,则根节点即为第K个元素

leftSize >=k, 则第K个元素在左子树中,

leftSize +1 <k, 则转换为在右子树中,寻找第K-left-1元素。

/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
* @return {number}
*/
var kthSmallest = function(root, k) {
if(root==null){
return 0;
}
var leftSize=nodeNum(root.left); if(k==leftSize+1){
return root.val;
}else if(k<leftSize+1){
return kthSmallest(root.left,k);
}else{
return kthSmallest(root.right,k-leftSize-1);
}
}; function nodeNum(root){
if(root==null){
return 0;
}else{
return 1+nodeNum(root.left)+nodeNum(root.right);
}
}

【树】Kth Smallest Element in a BST(递归)的更多相关文章

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

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

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

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

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

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

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

  6. LeetCode OJ 230. Kth Smallest Element in a BST

    Total Accepted: 46445 Total Submissions: 122594 Difficulty: Medium Given a binary search tree, write ...

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

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

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

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

随机推荐

  1. C#中验证sql语句是否正确(不执行语句)

    SET PARSEONLY检查每个 Transact-SQL 语句的语法并返回任何错误消息,但不编译和执行语句.SET PARSEONLY { ON | OFF }当 SET PARSEONLY 为 ...

  2. arcgis api for flex 开发入门(一)环境搭建

    http://www.cnblogs.com/wenjl520/archive/2009/06/02/1494514.html arcgis api for flex 开发入门(一)环境搭建arcgi ...

  3. Sql Server R8 密码问题及5102错误

    登录的两种方式: 集成登录连接:con = new SqlConnection("server=.\\SQLEXPRESS;database=db_news;Trusted_Connecti ...

  4. NC入门笔记

    简介: NC(全名NetCat),在网络界有"瑞士军刀"的美誉.它小巧而强悍,被设计成一个稳定的后门工具,能够直接由其它程序和脚本轻松驱动.同时,它也是一个功能强大的网络调试和探测 ...

  5. centos7 磁盘管理—— lvm的使用

    Linux用户安装Linux操作系统时遇到的一个常见的难以决定的问题就是如何正确地评估各分区大小,以分配合适的硬盘空间.普通的磁盘分区管理方式在逻辑分区划分好之后就无法改变其大小,当一个逻辑分区存放不 ...

  6. hdu 5032 不易发觉的树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=5032 给定一个1000x1000的点阵,m组询问,每次询问一个由(0,0).(x,0)点一以及从原点出发的方向向 ...

  7. The Real Meaning of Peace

    The Real Meaning of Peace There once was a king who offered a prize to the artist who would paint th ...

  8. Spring 注入集合类型

    定义了一个类: @Service public class StringTest implements CachedRowSet,SortedSet<String>,Cloneable @ ...

  9. EBS Archiving and Purging: You Know you need to

    A number of trends in the IT industry have contributed to the increasing size of ERP application dat ...

  10. web api 多版本控制重要的两个类

    1.版本路径替换 public class ReplaceVersionWithExactValueInPath : IDocumentFilter     {         public void ...