[LeetCode] Kth Smallest Element in a BST

简介: This link suggests a concise C++ recursive solution. The original code may be hard to understand at first and I have rewritten the code below.

This link suggests a concise C++ recursive solution. The original code may be hard to understand at first and I have rewritten the code below. You may need to run some examples with it to see how it works.

 1 class Solution {
 2 public:
 3     int kthSmallest(TreeNode* root, int k) {
 4         return smallest(root, k);
 5     }
 6 private:
 7     int smallest(TreeNode* node, int& k) {
 8         if (!node) return -1;
 9         int val = smallest(node -> left, k);
10         if (!k) return val;
11         if (!--k) return node -> val;
12         return smallest(node -> right, k);
13     }
14 };

The same author also posts three solutions which is more space-efficient in this link.  All of them reduce the O(n) space of the above code to O(k) space by using some nice data structures.

Personally I really love the soltuion using deque and I have rewritten it below.

 1 class Solution {
 2 public:
 3     int kthSmallest(TreeNode* root, int k) {
 4         TreeNode* node = root;
 5         deque<TreeNode*> nodes;
 6         while (true) {
 7             while (node) {
 8                 nodes.push_front(node);
 9                 if (nodes.size() > k)
10                     nodes.pop_back();
11                 node = node -> left;
12             }
13             node = nodes.front();
14             nodes.pop_front();
15             if (!--k) return node -> val;
16             node = node -> right;
17         }
18     }
19 };

 

目录
相关文章
|
5月前
Leetcode 230. Kth Smallest Element in a BST
先来看下二叉搜索树的性质,对于任意一个非叶子节点,它的左子树中所有的值必定小于这个节点的val,右子树中所有的值必定大于或等于当前节点的val。 这条性质就保证了如果我们对二叉搜索树做中序遍历,中序遍历的结果肯定是有序的。对于此题而言,我们只需要拿到中序遍历结果,然后返回第k个即可,时间复杂度是O(n)。
46 1
|
算法 Python
LeetCode 169. 多数元素 Majority Element
LeetCode 169. 多数元素 Majority Element
|
算法
LeetCode 229. Majority Element II
给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素。 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1)。
59 0
LeetCode 229. Majority Element II
|
索引
LeetCode 162. Find Peak Element
给定一个输入数组 nums,其中 nums[i] ≠ nums[i+1],找到峰值元素并返回其索引。 数组可能包含多个峰值,在这种情况下,返回任何一个峰值所在位置即可。
73 0
LeetCode 162. Find Peak Element
LeetCode 109. Convert Sorted List to BST
给定一个链表,其中元素按升序排序,将其转换为高度平衡的BST。 对于这个问题,高度平衡的二叉树被定义为:二叉树中每个节点的两个子树的深度从不相差超过1。
43 0
LeetCode 109. Convert Sorted List to BST
LeetCode 108. Convert Sorted Array to BST
给定一个数组,其中元素按升序排序,将其转换为高度平衡的BST。 对于这个问题,高度平衡的二叉树被定义为:二叉树中每个节点的两个子树的深度从不相差超过1。
52 0
LeetCode 108. Convert Sorted Array to BST
【LeetCode538】把二叉搜索树转换为累加树(BST中序)
一定注意有BST的条件,BST的特性是中序遍历(左中右)得到从小到大的序列,而题目求的是大于等于当前结点的值替换原值——注意这里是大于等于!!所以就不是单纯的中序,而是逆中序(右
85 0
【LeetCode538】把二叉搜索树转换为累加树(BST中序)
|
算法
​LeetCode刷题实战333:最大 BST 子树
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
127 0
|
Java C++
LeetCode之Remove Element
LeetCode之Remove Element
84 0
LeetCode之Next Greater Element I
LeetCode之Next Greater Element I
56 0

热门文章

最新文章