8.3 LeetCode230 Return the kth smallest element of a BST

Kth Smallest Element in a BST

Total Accepted: 9992 Total Submissions: 33819My Submissions

Question Solution

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?

Method: According to the property of BST, the inOrder order is just the ascending order. So insert the BST as inOrder into an ArrayList then return list(k-1).

public class Solution { // the inOrder sequence is the ascending order.
    ArrayList<Integer> list = new ArrayList<Integer>();
    public int kthSmallest(TreeNode root, int k) {
        insertArray(root);
        return list.get(k-1);
    }
    private void insertArray(TreeNode root) {
        if(root.left != null) insertArray(root.left);
        list.add(root.val);
        if(root.right != null) insertArray(root.right);
    }
}
时间: 2024-10-20 10:36:47

8.3 LeetCode230 Return the kth smallest element of a BST的相关文章

[LeetCode][JavaScript]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

【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

LeetCode-230. Kth Smallest Element in a BST

Description: 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小的数. 思路:中序遍历,找到第k个便是. C++: class Solution { public: int n

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

[Leetcode] 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. 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

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小的结点 思路: 只要明白BST树的原理,只要中序遍历一遍BST树即可.求第K小的,只需遍历前K个结点就OK. C++: 1 /*

Kth Smallest Element in a BST 解答

Question 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

[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