【树】Kth Smallest Element in a BST(递归)
题目:
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(递归)相关推荐
- 【LeetCode 剑指offer刷题】树题16:Kth Smallest Element in a BST
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Kth Smallest Element in a BST Given a binary search tree, ...
- [leetcode] 230. Kth Smallest Element in a BST 找出二叉搜索树中的第k小的元素
题目大意 https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 230. Kth Smallest Elem ...
- [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 ...
- 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 ...
- LeetCode:Kth Smallest Element in a BST
Kth Smallest Element in a BST Total Accepted: 49142 Total Submissions: 128501 Difficulty: Medium Giv ...
- 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 ...
- 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 ...
- 230. Kth Smallest Element in a BST
题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...
- 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 ...
最新文章
- 手动制作自己想的语谱图
- truelicense中的maven配置
- spring boot2.3.1版本导入spring-boot-starter-web没有validation校验框架的解决办法
- oracle 得到一个树,Related to Oracle SQL 关于树形数据的遍历
- python解释器可以使用什么命令_python解释器用什么写的
- 计算机粘贴功能不能用了,电脑复制粘贴不能用了【解决办法】
- 【数据结构笔记】B树和B+树的实现,哈希查找,STL中的hash_map和unordered_map容器用法
- PHP自学4——通过函数将数组数据输出到html的Table标签中(使用函数的例子)
- 测试驱动陷阱,第2部分
- python pip install syntaxerror_解决pip install xxx报错SyntaxError: invalid syntax的问题
- 转载:ADO.NET Entity Framework 试水系列索引(2008/9/9更新,本系列结束)
- Python开发人员最喜欢的工具
- iOS工作中的经验总结—马甲包审核以及常见审核问题!!!(干货)
- str.trim()去除空格
- for循环:100以内奇数之和
- ETL工具kettle实现数据同步
- matlab 存成bmp格式,MAT、BMP、PNG文件格式说明及MATLAB中文件的保存方式
- 【Linux】linux 终端报Message from syslogd
- centos7 silk转mp3 wav
- 月薪两万的“土豪”师兄,加个微信吧!