📖 Scenario: You are working with a simple phone book application that stores contacts in a special tree structure called a Binary Search Tree (BST). Each contact has a unique number. You want to find the contact with the kth smallest number quickly.
🎯 Goal: Build a program that creates a BST with given numbers, sets a value for k, and finds the kth smallest number in the BST.
📋 What You'll Learn
Create a BST by inserting nodes with given values
Set a variable k to specify which smallest element to find
Write a function to find the kth smallest element using in-order traversal
Print the kth smallest element
💡 Why This Matters
🌍 Real World
Finding the kth smallest element is useful in databases, search engines, and ranking systems where sorted data retrieval is needed.
💼 Career
Understanding BSTs and traversal algorithms is important for software engineers working on data storage, retrieval, and optimization tasks.
Progress0 / 4 steps
1
Create the BST Node class and insert nodes
Create a class called Node with an __init__ method that takes val and sets self.val, self.left, and self.right to None. Then create a function called insert that takes root and val and inserts a new node with val into the BST. Finally, create a BST by inserting these values in order: 5, 3, 6, 2, 4, 1. Store the root in a variable called root.
DSA Python
Hint
Remember, the insert function places smaller values to the left and larger or equal values to the right.
2
Set the value of k
Create a variable called k and set it to 3 to find the 3rd smallest element in the BST.
DSA Python
Hint
Just create a variable k and assign it the value 3.
3
Write function to find kth smallest element
Write a function called kth_smallest that takes root and k. Use an inner helper function called inorder to do an in-order traversal of the BST. Keep track of visited nodes in a list called elements. Return the kth smallest element from elements.
DSA Python
Hint
Use in-order traversal to visit nodes in ascending order and collect their values in a list.
4
Print the kth smallest element
Use print to display the result of calling kth_smallest(root, k).
DSA Python
Hint
Call the function and print its result to see the kth smallest value.
Practice
(1/5)
1. What does the term Kth Smallest Element in BST mean?
easy
A. The element that is the Kth largest in the BST
B. The element that is the Kth in ascending order in the BST
C. The root element of the BST
D. The element with value K in the BST
Solution
Step 1: Understand BST ordering
A Binary Search Tree (BST) stores elements so that left child is smaller and right child is larger.
Step 2: Define Kth smallest element
The Kth smallest element is the element that appears in the Kth position when all elements are sorted ascending.
Final Answer:
The element that is the Kth in ascending order in the BST -> Option B
Quick Check:
Kth smallest means sorted order position [OK]
Hint: Kth smallest means counting nodes in ascending order [OK]
Common Mistakes:
Confusing Kth smallest with Kth largest
Thinking it means the root node
Assuming K is the value, not position
2. Which of the following Python function signatures is correct to find the Kth smallest element in a BST?
easy
A. def kth_smallest(root: Optional[TreeNode], k: int) -> int:
B. def kth_smallest(k: int, root: TreeNode) -> int:
C. def kth_smallest(root: TreeNode, k: str) -> int:
D. def kth_smallest(root: TreeNode) -> int:
Solution
Step 1: Check parameter order and types
The function should take the root node and an integer k to find the Kth smallest element.
Step 2: Validate correct typing and order
def kth_smallest(root: Optional[TreeNode], k: int) -> int: correctly uses root as TreeNode (or Optional) and k as int, returning int.
Final Answer:
def kth_smallest(root: Optional[TreeNode], k: int) -> int: -> Option A
Quick Check:
Correct parameter order and types [OK]
Hint: Root node first, k as int second in function parameters [OK]
Common Mistakes:
Swapping parameter order
Using wrong type for k (like string)
Missing k parameter
3. Given the BST below and k = 3, what is the output of the function kth_smallest(root, 3)?