JavaScript solution


  • 1
    L
    function BSTIterator(root) {
      var stack = [];
      return {hasNext, next};
    
      function hasNext() {
        return root || stack.length;
      }
    
      function next() {
        while (root) {
          stack.push(root);
          root = root.left;
        }
        root = stack.pop();
        var result = root.val;
        root = root.right;
        return result;
      }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.