Kth Smallest Element in a BST

Total Accepted: 49142 Total Submissions: 128501 Difficulty: Medium

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:

  1. Try to utilize the property of a BST.
  2. What if you could modify the BST node's structure?
  3. 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

Hide Tags

Binary Search Tree

Hide Similar Problems

(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相关推荐

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

  2. LeetCode Kth Smallest Element in a BST(中序遍历)

    问题:给出一个二叉查找树,求第k小的数 思路:第一种方式是使用递归中序遍历得到结果后,直接取第k个数即可 第二种方式是使用非递归中序遍历,在得到第k个数后直接停止 具体代码参考: https://gi ...

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

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

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

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

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

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

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

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

最新文章

  1. Java项目:控制台商城系统(java+打印控制台)
  2. numpy nonzero 找出非0元素
  3. hdu 3064
  4. can a select block a truncate (ZT)
  5. MySQL InnoDB表压缩
  6. 年初新立Flag,新华三解决方案部做了点儿啥?
  7. SpringMVC 通过post接收form参数或者json参数
  8. Supervisor-类unix系统下的进程控制工具
  9. 王思聪旗下公司破产拍卖:13.8万成交、溢价超100倍
  10. 企业微信本地测试环境搭建
  11. python右对齐_Python中print函数输出时的左右对齐问题
  12. 关于Vue使用es6模板字符串没反应的问题
  13. NVMe-MI 时代的NVMe SSD监控和管理
  14. 《GTA5》游戏拆解分析
  15. 学校学生工科学生接私活_理工科学生在美国以外的最佳城市
  16. 精彩WAP之旅--上海热线WAP站
  17. 校招总结(华为、远景、新美大、百度、腾讯、网易游戏)
  18. 【Java专题】Java中的I/O流详解
  19. lammps案例:Fe原子辐照轰击多层石墨烯模拟
  20. 租房注意事项(北京)

热门文章

  1. APP-FND-01564: ORACLE error 24345 in fdlget
  2. 台湾南投3.27地震
  3. 两个案例带你了解 cookie 和 SSL(开心网 jobbole)
  4. ybt1248_Dungeon Master
  5. 软工网络15个人阅读作业1(201521123007谭燕)
  6. 10Gb每秒!SM4的单核“心”!海泰携手海量数据安全“闪”护
  7. 利用FastReport传递图片参数,在报表上展示签名信息
  8. 计算机视觉理论笔记 (3) - 图像采集 (Image Acquisition)
  9. DataWhale-VCED项目学习-2Jina
  10. 高代|共轭矩阵 自共轭矩阵