Home
/
Tutorials
/
You’ve learned a lot about BSTs. Let’s see how well you understand them.
Before the quiz, remember these key points:
BST Property : Left < Root < Right (recursively)
Search : O(log n) average, O(n) worst
Insertion : Find spot, add node
Deletion : Three cases (0, 1, or 2 children)
Traversal : In-order gives sorted values
Arrange these BST operations in the correct order for inserting values 50, 30, 70, 20:
Drag and Drop Activity This interactive activity requires JavaScript to be
enabled.
Items: Start at root → Step 1 Insert 50 (becomes root) → Step 2 Insert 30 (goes left of 50) → Step 3 Insert 70 (goes right of 50) → Step 4 Insert 20 (goes left of 30) → Step 5 Zones: Step 1 Step 2 Step 3 Step 4 Step 5
Knowledge Check This interactive quiz requires JavaScript to be enabled.
Question 1: What is the time complexity of searching in a balanced BST? A. O(1) B. O(log n) (Correct) C. O(n) D. O(n log n) Explanation: In a balanced BST, search eliminates half the tree at each step, giving O(log n) time complexity.
Question 2: What does in-order traversal of a BST produce? A. Values in random order B. Values in sorted order (Correct) C. Values in reverse sorted order D. Values in level order Explanation: In-order traversal (left → root → right) visits nodes in sorted order because of the BST property.
Question 3: When deleting a node with two children, what do we use as replacement? A. The maximum value in the left subtree B. The minimum value in the right subtree (Correct) C. The root of the tree D. Any leaf node Explanation: We use the in-order successor (minimum in right subtree) to maintain the BST property.
Question 4: Which of these violates the BST property? A. Left child < Parent < Right child B. All left subtree values < Root < All right subtree values C. A value in left subtree is greater than root (Correct) D. Each node has at most two children Explanation: If a value in the left subtree is greater than the root, it violates the BST property.
Question 5: What is the worst-case time complexity for BST operations? A. O(log n) B. O(n) (Correct) C. O(n log n) D. O(1) Explanation: In the worst case (unbalanced tree like a linked list), operations take O(n) time.
Try implementing a function to check if a tree is a valid BST:
Run code to see output...
You’ve mastered:
✅ BST Structure : Nodes with left and right pointers
✅ BST Property : Left < Root < Right
✅ Insertion : Find spot, add node
✅ Search : Follow BST property to find value
✅ Deletion : Handle three cases correctly
✅ Traversal : In-order, pre-order, post-order
✅ Time Complexity : O(log n) average, O(n) worst
With BST knowledge, you can:
Implement efficient search in your applications
Build ordered data structures
Understand how databases index data
Solve tree-based algorithm problems
Design efficient data storage
Practice with more BST problems on LeetCode
Learn about self-balancing trees (AVL, Red-Black)
Explore B-trees used in databases
Study tree algorithms (lowest common ancestor, etc.)
Great job completing this tutorial!