关闭

LeetCode:Kth Smallest Element in a BST

标签: LeetCodeKth Smallest Element
719人阅读 评论(1) 收藏 举报
分类:

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 + 1
            return kthSmallest(root.right, k - count - 1);
            
    }
    
    // 自定义函数:计算树中结点个数
    int countNode(TreeNode root) {
        if(root == null) return 0;
        
        return countNode(root.left) + countNode(root.right) + 1;
    }
    
}

0
0
查看评论

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. Note: You may assume k is always valid, 1 ≤ k ≤ BST&...
  • sunao2002002
  • sunao2002002
  • 2015-07-02 14:27
  • 5601

leetcode 230: Kth Smallest Element in a BST

leetcode 230: Kth Smallest Element in a BST python java c++
  • xudli
  • xudli
  • 2015-07-07 06:14
  • 9358

LeetCode 230: Kth Smallest Element in a BST 查找二叉排序树

博客转载请注明地址:题目描述题目地址:https://leetcode.com/problems/kth-smallest-element-in-a-bst/ 在一颗二叉排序树当中,寻找第k大的数。考察点 递归 树的遍历 陷阱无Code/** * Definition for a binary ...
  • SunliyMonkey
  • SunliyMonkey
  • 2015-09-02 15:34
  • 1308

leetcode 230. Kth Smallest Element in a BST-递归|非递归

原题链接:230. Kth Smallest Element in a BST 【思路-Java、Python】——递归实现 我们知道二分查找数(BST)的性质——任何一个节点的值均大于左子树的任意节点值,而小于右子树的任一节点值。那么这样就可以知道最小值的一个节点在树的最左端,最大值的一个节点在树...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-05-24 11:17
  • 1090

LeetCode 230. Kth Smallest Element in a BST 解题报告

230. Kth Smallest Element in a BST My Submissions Question Total Accepted: 35869 Total Submissions: 100057 Difficulty:&...
  • bruce128
  • bruce128
  • 2016-02-16 15:27
  • 1543

leetcode230:Kth Smallest Element in a BST(medium)

在上一章博文中记录了leetcode第378道题(详细内容查看上一章),该题是返回一个矩阵中第k小的元素,与该题类似,这道题是返回一个二叉搜索树中第k小的元素。 题目:Given a binary search tree, write a function kthSmallest ...
  • qq_18297933
  • qq_18297933
  • 2016-08-21 21:27
  • 291

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

原题网址:https://leetcode.com/problems/kth-smallest-element-in-a-bst/ Given a binary search tree, write a function kthSmallest to find the...
  • jmspan
  • jmspan
  • 2016-04-05 11:16
  • 633

LeetCode—378. Kth Smallest Element in a Sorted Matrix

Kth Smallest Element in a Sorted Matrix思路:1.首先这题把所有的数组遍历出来然后直接排序就可以AC..... 2.正常思路就是维系最大堆。 GitHub地址:https://github.com/corpsepiges/leetcode ...
  • corpsepiges
  • corpsepiges
  • 2016-08-02 10:20
  • 2146

LeetCode 378. Kth Smallest Element in a Sorted Matrix 解题报告

LeetCode 378. Kth Smallest Element in a Sorted Matrix 解题报告
  • camellhf
  • camellhf
  • 2016-10-07 23:29
  • 339

【LeetCode】Kth Smallest Element in a BST 解题报告

【LeetCode】Kth Smallest Element in a BST 解题报告标签(空格分隔): LeetCode题目地址:https://leetcode.com/problems/kth-smallest-element-in-a-bst/#/description题目描述:Given...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2017-04-10 14:28
  • 192
    个人资料
    • 访问:389687次
    • 积分:6783
    • 等级:
    • 排名:第4134名
    • 原创:321篇
    • 转载:1篇
    • 译文:0篇
    • 评论:34条
    博客专栏
    最新评论