← Back to list

[LeetCode] 230. Kth Smallest Element in a BST

Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the…

Jason Xiao · 2022-04-18 15:32 · 0 claps · 0.8 min read
#leetcode #leetcode-medium #leetcode-solution
Open on Medium ↗

[LeetCode] 230. Kth Smallest Element in a BST

Given the root of a binary search tree, and an integer k, return the kth smallest value (1-indexed) of all the values of the nodes in the tree.

Example 1:

Input: root = [3,1,4,null,2], k = 1
Output: 1

Example 2:

Input: root = [5,3,6,2,4,null,null,1], k = 3
Output: 3

Constraints:

  • The number of nodes in the tree is n.
  • 1 <= k <= n <= 104
  • 0 <= Node.val <= 104

Ans

[embed]

Solution

看到 BST 就知道老東西了

實際上這題就是考 Binary Tree Traversal 的方法概念

(Inorder, Preorder, Postorder)

因為是老東西了 我就不介紹了

如果有刷 LeetCode Easy 題目就知道

94. Binary Tree Inorder Traversal


메타데이터
post_id
1094be4fc17e
slug
leetcode-230-kth-smallest-element-in-a-bst-1094be4fc17e
url
https://medium.com/@kosjason/leetcode-230-kth-smallest-element-in-a-bst-1094be4fc17e
canonical_url
https://medium.com/@kosjason/leetcode-230-kth-smallest-element-in-a-bst-1094be4fc17e
author_url
https://medium.com/@kosjason
status
ok
fetched_at
2026-07-27 06:20:56