Nav: << previous: 229.多数元素 II | next: 231.2 的幂 >>


Description

tab: English
 
<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>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>Input:</strong> root = [3,1,4,null,2], k = 1
<strong>Output:</strong> 1
</pre>
 
<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>Input:</strong> root = [5,3,6,2,4,null,null,1], k = 3
<strong>Output:</strong> 3
</pre>
 
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>
 
<ul>
	<li>The number of nodes in the tree is <code>n</code>.</li>
	<li><code>1 &lt;= k &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
</ul>
 
<p>&nbsp;</p>
<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>
 
 
 
> [!tip]- Hint 1
> 
> Try to utilize the property of a BST.
 
> [!tip]- Hint 2
> 
> Try in-order traversal. (Credits to @chan13)
 
> [!tip]- Hint 3
> 
> What if you could modify the BST node's structure?
 
> [!tip]- Hint 4
> 
> The optimal runtime complexity is O(height of BST).
 
 
---
 
[submissions](https://leetcode.com/problems/kth-smallest-element-in-a-bst/submissions/) | [solutions](https://leetcode.com/problems/kth-smallest-element-in-a-bst/solutions/)
 
 
tab: 中文
 
<p>给定一个二叉搜索树的根节点 <code>root</code> ,和一个整数 <code>k</code> ,请你设计一个算法查找其中第&nbsp;<code>k</code><strong>&nbsp;</strong>小的元素(从 1 开始计数)。</p>
 
<p>&nbsp;</p>
 
<p><strong>示例 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg" style="width: 212px; height: 301px;" />
<pre>
<strong>输入:</strong>root = [3,1,4,null,2], k = 1
<strong>输出:</strong>1
</pre>
 
<p><strong>示例 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg" style="width: 382px; height: 302px;" />
<pre>
<strong>输入:</strong>root = [5,3,6,2,4,null,null,1], k = 3
<strong>输出:</strong>3
</pre>
 
<p>&nbsp;</p>
 
<p>&nbsp;</p>
 
<p><strong>提示:</strong></p>
 
<ul>
	<li>树中的节点数为 <code>n</code> 。</li>
	<li><code>1 &lt;= k &lt;= n &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
</ul>
 
<p>&nbsp;</p>
 
<p><strong>进阶:</strong>如果二叉搜索树经常被修改(插入/删除操作)并且你需要频繁地查找第 <code>k</code> 小的值,你将如何优化算法?</p>
 
 
 
> [!tip]- 提示 1
> 
> Try to utilize the property of a BST.
 
> [!tip]- 提示 2
> 
> Try in-order traversal. (Credits to @chan13)
 
> [!tip]- 提示 3
> 
> What if you could modify the BST node's structure?
 
> [!tip]- 提示 4
> 
> The optimal runtime complexity is O(height of BST).
 
 
---
 
[提交记录](https://leetcode.cn/problems/kth-smallest-element-in-a-bst/submissions/) | [题解](https://leetcode.cn/problems/kth-smallest-element-in-a-bst/solution/)
 
 

Solutions & Notes

properties:
  note.updated:
    displayName: Last Updated
  note.relative_links:
    displayName: Related Links
  note.desc:
    displayName: Description
  note.grade:
    displayName: Rating
  note.program_language:
    displayName: Language
  note.time_complexity:
    displayName: TC
  note.space_complexity:
    displayName: SC
views:
  - type: table
    name: Solutions & Notes
    filters:
      and:
        - file.hasLink(this.file)
        - file.tags.containsAny("leetcode/solution", "leetcode/note")
    order:
      - file.name
      - desc
      - program_language
      - time_complexity
      - space_complexity
      - grade
      - relative_links
      - updated
    sort:
      - property: grade
        direction: ASC
      - property: time_complexity
        direction: ASC
      - property: program_language
        direction: ASC
    columnSize:
      file.name: 104
      note.space_complexity: 65
      note.grade: 126
 

Similar Problems

properties:
  note.lcTopics:
    displayName: Topics
  note.lcAcRate:
    displayName: AC Rate
  note.favorites:
    displayName: Favorites
  note.grade:
    displayName: Rating
  note.translatedTitle:
    displayName: Title (CN)
  note.lcDifficulty:
    displayName: Difficulty
views:
  - type: table
    name: Similar Problems
    filters:
      and:
        - file.hasLink(this.file)
        - similarQuestions.contains(this.file)
    order:
      - file.name
      - translatedTitle
      - lcTopics
      - lcDifficulty
      - lcAcRate
      - grade
      - favorites
    sort:
      - property: file.name
        direction: ASC
      - property: lcTopics
        direction: DESC
    columnSize:
      note.translatedTitle: 240
      note.lcTopics: 347
      note.lcAcRate: 75
      note.grade: 122