Leetcode 230: Kth Smallest Element in a BST
Given the root of a binary search tree and an integer k, your task is to return the kth smallest value in the tree (1-indexed).
Problem
Approach
Steps
Complexity
Input: The input consists of the root node of a binary search tree (BST), represented as a tree structure. The second input is an integer k (1 <= k <= n), where n is the number of nodes in the tree.
Example: Input: root = [7, 3, 10, 2, 5, null, 15], k = 2
Constraints:
• 1 <= k <= n <= 10^4
• 0 <= Node.val <= 10^4
Output: The output should be a single integer representing the kth smallest value in the binary search tree.
Example: Output: 3
Constraints:
Goal: The goal is to find the kth smallest element in the binary search tree by leveraging the properties of the binary search tree and performing an in-order traversal.
Steps:
• Perform an in-order traversal of the binary search tree.
• Keep track of the number of nodes visited during the traversal.
• Return the value of the kth node when the counter reaches k.
Goal: The solution should efficiently handle up to 10^4 nodes.
Steps:
Assumptions:
• The tree is a valid binary search tree.
• There are no duplicate values in the tree.
• Input: Input: root = [7, 3, 10, 2, 5, null, 15], k = 2
• Explanation: In this tree, the inorder traversal would give the sequence: [2, 3, 5, 7, 10, 15]. The second smallest value is 3, so the output is 3.
• Input: Input: root = [4, 2, 6, 1, 3, 5, 7], k = 3
• Explanation: The inorder traversal of the tree gives: [1, 2, 3, 4, 5, 6, 7]. The third smallest value is 3, so the output is 3.
Approach: To find the kth smallest value in a binary search tree, an in-order traversal can be used since it visits nodes in ascending order for a BST.
Observations:
• In-order traversal of a BST gives the values in sorted order.
• By counting the nodes during the traversal, we can identify when we reach the kth smallest element.
Steps:
• Start with the root node of the binary search tree.
• Use a stack to implement an iterative in-order traversal or use recursion.
• Track the number of nodes visited.
• When the counter reaches k, return the value of the current node.
Empty Inputs:
• If the root is null, there are no elements to traverse, and the problem should return an error or invalid result.
Large Inputs:
• For large trees, ensure the solution can handle up to 10^4 nodes without performance degradation.
Special Values:
• If the tree has a single node, that node will be the kth smallest for any k = 1.
Constraints:
• The solution must work within time limits for up to 10^4 nodes.
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> stk;
// stk.push(root);
TreeNode* node= root;
while(!stk.empty() || node) {
if(node) {
stk.push(node);
node = node->left;
} else {
node = stk.top();
stk.pop();
k--;
if(k == 0) return node->val;
node = node->right;
}
}
return NULL;
}
1 : Function Definition
int kthSmallest(TreeNode* root, int k) {
Defines the function `kthSmallest` that takes the root of a binary tree and the value `k`, and returns the k-th smallest element in the BST.
2 : Variable Initialization
stack<TreeNode*> stk;
Initializes a stack `stk` to assist with the in-order traversal of the tree.
3 : Variable Initialization
TreeNode* node= root;
Assigns the root of the tree to the `node` pointer for traversal.
4 : Loop Control
while(!stk.empty() || node) {
Begins a `while` loop that runs until both the stack is empty and there are no more nodes to visit.
5 : Condition Check
if(node) {
Checks if the current `node` is not null, meaning there is still a node to visit.
6 : Push Node
stk.push(node);
Pushes the current node onto the stack to process it later.
7 : Traverse Left
node = node->left;
Moves to the left child of the current node for further traversal.
8 : Else Condition
} else {
If the current node is null, it means we've reached the leftmost node and can start processing nodes from the stack.
9 : Pop Node
node = stk.top();
Pops the top node from the stack, which is the next node in the in-order traversal.
10 : Pop Node
stk.pop();
Removes the node from the stack after it has been processed.
11 : Decrement k
k--;
Decrements the value of `k` as we move closer to finding the k-th smallest element.
12 : Kth Element Check
if(k == 0) return node->val;
Checks if the current node is the k-th smallest element. If so, returns its value.
13 : Traverse Right
node = node->right;
Moves to the right child of the current node to continue the in-order traversal.
14 : Return Null
return NULL;
Returns `NULL` if no k-th smallest element is found, which should never happen for a valid input.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is O(n) because the traversal must visit every node in the tree at least once.
Best Case: O(h)
Worst Case: O(h)
Description: The space complexity is O(h), where h is the height of the tree, due to the stack used in the traversal (for the recursive solution, it depends on the tree's height).
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus