0
0
DSA Javascriptprogramming~20 mins

BST Iterator Design in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
BST Iterator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of BST Iterator next() calls
Given the BST Iterator initialized with the BST below, what is the output sequence of calling next() three times?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

class BSTIterator {
  constructor(root) {
    this.stack = [];
    this._leftmostInorder(root);
  }

  _leftmostInorder(node) {
    while (node !== null) {
      this.stack.push(node);
      node = node.left;
    }
  }

  next() {
    const topNode = this.stack.pop();
    if (topNode.right !== null) {
      this._leftmostInorder(topNode.right);
    }
    return topNode.val;
  }

  hasNext() {
    return this.stack.length > 0;
  }
}

// BST structure:
//       7
//      / \
//     3   15
//         / \
//        9  20

const root = new TreeNode(7,
  new TreeNode(3),
  new TreeNode(15, new TreeNode(9), new TreeNode(20))
);
const iterator = new BSTIterator(root);

const output = [iterator.next(), iterator.next(), iterator.next()];
console.log(output);
A[3, 7, 9]
B[7, 3, 9]
C[3, 9, 7]
D[7, 9, 15]
Attempts:
2 left
💡 Hint
Remember, BST Iterator returns nodes in ascending order using in-order traversal.
🧠 Conceptual
intermediate
1:00remaining
Understanding BST Iterator hasNext() behavior
What does the hasNext() method of a BST Iterator indicate during iteration?
AIt returns true if the stack is empty.
BIt returns true if the current node has a right child.
CIt returns true if there are more nodes to visit in ascending order.
DIt returns true if the BST is balanced.
Attempts:
2 left
💡 Hint
Think about what the stack in the iterator represents.
🔧 Debug
advanced
1:30remaining
Identify the error in BST Iterator next() method
What error will occur when running the next() method in the following BST Iterator code snippet?
DSA Javascript
class BSTIterator {
  constructor(root) {
    this.stack = [];
    this._leftmostInorder(root);
  }

  _leftmostInorder(node) {
    while (node !== null) {
      this.stack.push(node);
      node = node.left;
    }
  }

  next() {
    const topNode = this.stack.pop();
    if (topNode.right !== null) {
      this._leftmostInorder(topNode.right);
    }
    return topNode.val;
  }

  hasNext() {
    return this.stack.length > 0;
  }
}

const iterator = new BSTIterator(null);
iterator.next();
ANo error, returns null
BTypeError: Cannot read property 'right' of undefined
CReferenceError: topNode is not defined
DRangeError: Maximum call stack size exceeded
Attempts:
2 left
💡 Hint
Consider what happens when the BST is empty and next() is called.
🚀 Application
advanced
0:45remaining
Number of next() calls to traverse entire BST
If a BST Iterator is initialized with a BST of 10 nodes, how many times must next() be called to visit all nodes?
A10
B9
C11
DDepends on the shape of the BST
Attempts:
2 left
💡 Hint
Each next() call returns one node's value.
🧠 Conceptual
expert
1:30remaining
Space complexity of BST Iterator
What is the worst-case space complexity of a BST Iterator implemented with a stack for a BST with n nodes?
AO(1), constant space
BO(n), where n is the number of nodes in the BST
CO(log n), assuming the BST is balanced
DO(h), where h is the height of the BST
Attempts:
2 left
💡 Hint
Consider how many nodes can be on the stack at once during traversal.