Javascript solution


  • 0
    E
    var BSTSerializer = function(root,lst) {
        if(root) { 
            BSTSerializer(root.left,lst);
            lst.push(root.val);
            BSTSerializer(root.right,lst);
        }
    };
    var BSTIterator = function(root) {
        var lst=[];
        BSTSerializer(root,lst);
        this.items=lst;
        this.len=this.items.length;
        this.curr=0;
    };
    BSTIterator.prototype.hasNext = function() {
        return this.curr<this.len;
    };
    BSTIterator.prototype.next = function() {
        return this.items[this.curr++];
    };

Log in to reply
 

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