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

链接:  http://leetcode.com/problems/kth-smallest-element-in-a-bst/

题解:

用了比较笨的方法 - inorder traversal,做了一下剪枝。

Time Complexity - O(k), Space Complexity - O(k)。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    List<Integer> nodeVal = new ArrayList<Integer>();

    public int kthSmallest(TreeNode root, int k) {
        if(root == null)
            return 0;
        inorder(root, k);
        return nodeVal.get(k - 1);
    }

    private void inorder(TreeNode root, int k) {
        if(nodeVal.size() >= k)
            return;
        if(root == null)
            return;
        inorder(root.left, k);
        nodeVal.add(root.val);
        inorder(root.right, k);

    }
}

Update:

Time Complexity - O(n), Space Complexity - O(1)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {

    private int count = 0;
    private int res = 0;
    public int kthSmallest(TreeNode root, int k) {
        if(root == null)
            return 0;
        inorder(root, k);
        return res;
    }

    private void inorder(TreeNode root, int k) {
        if(root == null)
            return;
        if(count >= k)
            return;
        inorder(root.left, k);
        if(count >= k)
            return;
        res = root.val;
        count++;
        inorder(root.right, k);

    }
}

Reference:

https://leetcode.com/discuss/43267/4-lines-in-c

https://leetcode.com/discuss/43464/what-if-you-could-modify-the-bst-nodes-structure

https://leetcode.com/discuss/43299/o-k-space-o-n-time-10-short-lines-3-solutions

https://leetcode.com/discuss/45684/share-my-c-iterative-alg

https://leetcode.com/discuss/43771/implemented-java-binary-search-order-iterative-recursive

时间: 2024-08-05 23:41:33

230. Kth Smallest Element in a BST的相关文章

【LeetCode】230. Kth Smallest Element in a BST (2 solutions)

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

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

[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's total elements. 问题:找出二叉搜索树种第 k 小的元素. 一个深度遍历的应用.使用递归.或者借助栈都可以实现深度遍历.本文代码使用递归实现. 1 void visit(TreeNod

(medium)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's total elements. Follow up:What if the BST is modified (insert/delete operations) often and you need

[leedcode 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's total elements. Follow up:What if the BST is modified (insert/delete operations) often and you need

[LC] 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's total elements. Example 1: Input: root = [3,1,4,null,2], k = 1 3 / 1 4   2 Output: 1 Example 2: Inpu

Java for LeetCode 230 Kth Smallest Element in a BST

解题思路: 直接修改中序遍历函数即可,JAVA实现如下: int res = 0; int k = 0; public int kthSmallest(TreeNode root, int k) { this.k = k; inorderTraversal(root); return res; } public List<Integer> inorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<

230 Kth Smallest Element in a BST 二叉搜索树中第K小的元素

给定一个二叉搜索树,编写一个函数kthSmallest来查找其中第k个最小的元素. 注意:你可以假设k总是有效的,1≤ k ≤二叉搜索树元素个数. 进阶:如果经常修改二叉搜索树(插入/删除操作)并且你需要频繁地找到第k小值呢? 你将如何优化kthSmallest函数? 详见:https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 方法一:递归实现 /** * Definition for a binary

LeetCode 230. Kth Smallest Element in a BST 动态演示

返回排序二叉树第K小的数 还是用先序遍历,记录index和K进行比较 class Solution { public: void helper(TreeNode* node, int& idx, int k, int& res){ if(res!=INT_MAX) return; if(!node) return; //a(node) //lk("root",node) //dsp helper(node->left, idx, k, res); if(idx==