题目:

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);}
}

转载于:https://www.cnblogs.com/shytong/p/5169013.html

【树】Kth Smallest Element in a BST(递归)相关推荐

  1. 【LeetCode 剑指offer刷题】树题16:Kth Smallest Element in a BST

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Kth Smallest Element in a BST Given a binary search tree, ...

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

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

  5. LeetCode:Kth Smallest Element in a BST

    Kth Smallest Element in a BST Total Accepted: 49142 Total Submissions: 128501 Difficulty: Medium Giv ...

  6. 230. Kth Smallest Element in a BST ——迭代本质:a=xx1 while some_condition: a=xx2

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

  7. Leetcode - 230. Kth Smallest Element in a BST (BST)

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

  8. 230. Kth Smallest Element in a BST

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

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

最新文章

  1. 手动制作自己想的语谱图
  2. truelicense中的maven配置
  3. spring boot2.3.1版本导入spring-boot-starter-web没有validation校验框架的解决办法
  4. oracle 得到一个树,Related to Oracle SQL 关于树形数据的遍历
  5. python解释器可以使用什么命令_python解释器用什么写的
  6. 计算机粘贴功能不能用了,电脑复制粘贴不能用了【解决办法】
  7. 【数据结构笔记】B树和B+树的实现,哈希查找,STL中的hash_map和unordered_map容器用法
  8. PHP自学4——通过函数将数组数据输出到html的Table标签中(使用函数的例子)
  9. 测试驱动陷阱,第2部分
  10. python pip install syntaxerror_解决pip install xxx报错SyntaxError: invalid syntax的问题
  11. 转载:ADO.NET Entity Framework 试水系列索引(2008/9/9更新,本系列结束)
  12. Python开发人员最喜欢的工具
  13. iOS工作中的经验总结—马甲包审核以及常见审核问题!!!(干货)
  14. str.trim()去除空格
  15. for循环:100以内奇数之和
  16. ETL工具kettle实现数据同步
  17. matlab 存成bmp格式,MAT、BMP、PNG文件格式说明及MATLAB中文件的保存方式
  18. 【Linux】linux 终端报Message from syslogd
  19. centos7 silk转mp3 wav
  20. 月薪两万的“土豪”师兄,加个微信吧!

热门文章

  1. Android之getSystemService
  2. Hadoop Hive sql语法详解
  3. HDU2724 Tree【最小生成树】
  4. Java中的Set集合类
  5. linux的同步与互斥
  6. java基础之堆、栈、方法区 继承 多态
  7. C++中的string 类型占几个字节
  8. oracle:sql约束
  9. 前端学习记录(CSS篇)
  10. 去除桌面图标蓝底的方法步骤