java.lang.Object
g0201_0300.s0230_kth_smallest_element_in_a_bst.Solution

public class Solution extends Object
230 - Kth Smallest Element in a BST.<p>Medium</p> <p>Given the <code>root</code> of a binary search tree, and an integer <code>k</code>, return <em>the</em> <code>k<sup>th</sup></code> <em>smallest value ( <strong>1-indexed</strong> ) of all the values of the nodes in the tree</em>.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" alt="" /></p> <p><strong>Input:</strong> root = [3,1,4,null,2], k = 1</p> <p><strong>Output:</strong> 1</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" alt="" /></p> <p><strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3</p> <p><strong>Output:</strong> 3</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the tree is <code>n</code>.</li> <li><code>1 <= k <= n <= 10<sup>4</sup></code></li> <li><code>0 <= Node.val <= 10<sup>4</sup></code></li> </ul> <p><strong>Follow up:</strong> If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?</p>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • kthSmallest

      public int kthSmallest(TreeNode root, int k)