bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 173. Binary Search Tree Iterator

173. Binary Search Tree Iterator

Leetcode Solutions

Approach: Controlled Recursion

  1. Initialize an empty stack S.
  2. Define a helper function _inorder_left that adds all the nodes in the leftmost branch of the tree rooted at a given node to the stack.
  3. Call _inorder_left with the root node to start the inorder traversal.
  4. Implement hasNext() to return True if the stack is not empty, indicating that there are more elements to traverse.
  5. Implement next() to pop the top element from the stack, which is the next smallest element, and return its value. If the popped node has a right child, call _inorder_left with the right child to add the next set of nodes to the stack.
UML Thumbnail

Approach: Flattening the BST

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...