BST Iterator Design
📖 Scenario: Imagine you have a large collection of numbers stored in a Binary Search Tree (BST). You want to look at these numbers one by one in sorted order without loading them all at once. This is like flipping through pages of a book, seeing one page at a time.
🎯 Goal: You will build a BSTIterator in Go that lets you move through the BST in order, one number at a time. This iterator will have two main actions: Next() to get the next smallest number, and HasNext() to check if there are more numbers to see.
📋 What You'll Learn
Create a BST node structure called
TreeNode with Val, Left, and Right fieldsCreate a
BSTIterator struct with a stack to help traverse the treeImplement a constructor function
Constructor(root *TreeNode) BSTIterator to initialize the iteratorImplement
Next() int method to return the next smallest numberImplement
HasNext() bool method to check if more numbers are availableUse a stack to simulate the in-order traversal without recursion
💡 Why This Matters
🌍 Real World
BST iterators are used in databases and search engines to efficiently access sorted data without loading everything at once.
💼 Career
Understanding BST iterators helps in roles involving data structure optimization, backend development, and systems programming.
Progress0 / 4 steps