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 kth smallest element in it.
Note:
You may assume k is always valid, 1 ≤ k ≤ BST's total elements.
Follow up:
What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? How would you optimize the kthSmallest routine?
Hint:
- Try to utilize the property of a BST.
- What if you could modify the BST node's structure?
- The optimal runtime complexity is O(height of BST).
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
Binary Search Tree
(M) Binary Tree Inorder Traversal
java code:
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode(int x) { val = x; }* }*/
public class Solution {public int kthSmallest(TreeNode root, int k) {int count = countNode(root.left);if(k == count + 1)return root.val;else if(k < count + 1)return kthSmallest(root.left, k);else // k > count + 1return kthSmallest(root.right, k - count - 1);}// 自定义函数:计算树中结点个数int countNode(TreeNode root) {if(root == null) return 0;return countNode(root.left) + countNode(root.right) + 1;}}
LeetCode:Kth Smallest Element in a BST相关推荐
- [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 ...
- LeetCode Kth Smallest Element in a BST(中序遍历)
问题:给出一个二叉查找树,求第k小的数 思路:第一种方式是使用递归中序遍历得到结果后,直接取第k个数即可 第二种方式是使用非递归中序遍历,在得到第k个数后直接停止 具体代码参考: https://gi ...
- [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 剑指offer刷题】树题16:Kth Smallest Element in a BST
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) Kth Smallest Element in a BST Given a binary search tree, ...
- 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 - 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 ...
- 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 ...
- 230. Kth Smallest Element in a BST
题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. ...
- 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 ...
最新文章
- Java项目:控制台商城系统(java+打印控制台)
- numpy nonzero 找出非0元素
- hdu 3064
- can a select block a truncate (ZT)
- MySQL InnoDB表压缩
- 年初新立Flag,新华三解决方案部做了点儿啥?
- SpringMVC 通过post接收form参数或者json参数
- Supervisor-类unix系统下的进程控制工具
- 王思聪旗下公司破产拍卖:13.8万成交、溢价超100倍
- 企业微信本地测试环境搭建
- python右对齐_Python中print函数输出时的左右对齐问题
- 关于Vue使用es6模板字符串没反应的问题
- NVMe-MI 时代的NVMe SSD监控和管理
- 《GTA5》游戏拆解分析
- 学校学生工科学生接私活_理工科学生在美国以外的最佳城市
- 精彩WAP之旅--上海热线WAP站
- 校招总结(华为、远景、新美大、百度、腾讯、网易游戏)
- 【Java专题】Java中的I/O流详解
- lammps案例:Fe原子辐照轰击多层石墨烯模拟
- 租房注意事项(北京)
热门文章
- APP-FND-01564: ORACLE error 24345 in fdlget
- 台湾南投3.27地震
- 两个案例带你了解 cookie 和 SSL(开心网 jobbole)
- ybt1248_Dungeon Master
- 软工网络15个人阅读作业1(201521123007谭燕)
- 10Gb每秒!SM4的单核“心”!海泰携手海量数据安全“闪”护
- 利用FastReport传递图片参数,在报表上展示签名信息
- 计算机视觉理论笔记 (3) - 图像采集 (Image Acquisition)
- DataWhale-VCED项目学习-2Jina
- 高代|共轭矩阵 自共轭矩阵