关闭

230. Kth Smallest Element in a BST

标签: Kth Smallest ElementleetcodeBST
214人阅读 评论(0) 收藏 举报
分类:

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.

 

这又是一道关于二叉搜索树 Binary Search Tree 的题, LeetCode中关于BST的题有Validate Binary Search Tree 验证二叉搜索树, Recover Binary Search Tree 复原二叉搜索树, Binary Search Tree Iterator 二叉搜索树迭代器, Unique Binary Search Trees 独一无二的二叉搜索树, Unique Binary Search Trees II 独一无二的二叉搜索树之二Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树 和 Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树。那么这道题给的提示是让我们用BST的性质来解题,最重要的性质是就是左<根<右,那么如果用中序遍历所有的节点就会得到一个有序数组。所以解题的关键还是中序遍历啊。关于二叉树的中序遍历可以参见我之前的博客Binary Tree Inorder Traversal 二叉树的中序遍历,里面有很多种方法可以用,我们先来看一种非递归的方法:

解法一

复制代码
class Solution {
public:
    int kthSmallest(TreeNode* root, int k) {
        int cnt = 0;
        stack<TreeNode*> s;
        TreeNode *p = root;
        while (p || !s.empty()) {
            while (p) {
                s.push(p);
                p = p->left;
            }
            p = s.top(); s.pop();
            ++cnt;
            if (cnt == k) return p->val;
            p = p->right;
        }
        return 0;
    }
};
复制代码

 

当然,此题我们也可以用递归来解,还是利用中序遍历来解,代码如下:

解法二:

复制代码
class Solution {
public:
    int kthSmallest(TreeNode* root, int k) {
        return kthSmallestDFS(root, k);
    }
    int kthSmallestDFS(TreeNode* root, int &k) {
        if (!root) return -1;
        int val = kthSmallestDFS(root->left, k);
        if (!k) return val;
        if (!--k) return root->val;
        return kthSmallestDFS(root->right, k);
    }
};
复制代码

 

参考资料:

https://leetcode.com/discuss/43275/use-divide-and-conquer-to-solve-this-problem

 

LeetCode All in One 题目讲解汇总(持续更新中...)

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
  • 5598

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
  • 9356

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
  • 1087

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

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
  • 1305

LeetCode-230. Kth Smallest Element in a BST (JAVA)二叉树第k小的数字

LeetCode-230. Kth Smallest Element in a BST (JAVA)二叉树第k小的数字
  • mine_song
  • mine_song
  • 2017-04-29 10:36
  • 212

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 ( JAVA )

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note:  You may assume k is alwa...
  • UckyK
  • UckyK
  • 2016-04-11 17:33
  • 658

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
  • 631

【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
  • 190
    个人资料
    • 访问:82238次
    • 积分:2331
    • 等级:
    • 排名:第18868名
    • 原创:149篇
    • 转载:71篇
    • 译文:0篇
    • 评论:3条
    最新评论