You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */functionbuildStack(root,stack){if(root.right){buildStack(root.right,stack);}stack.push(root.value);if(root.left){buildStack(root.left,stack);}}/** * @param {TreeNode} root */varBSTIterator=function(root){varstack=[];buildStack(root,stack);this.stack=stack;};/** * @return the next smallest number * @return {number} */BSTIterator.prototype.next=function(){returnthis.stack.pop();};/** * @return whether we have a next smallest number * @return {boolean} */BSTIterator.prototype.hasNext=function(){returnthis.stack.length>0;};/** * Your BSTIterator object will be instantiated and called as such: * var obj = Object.create(BSTIterator).createNew(root) * var param_1 = obj.next() * var param_2 = obj.hasNext() */
The text was updated successfully, but these errors were encountered:
习题
思路
我们使用一个堆栈来保存所有的节点的值,保存的方式按照root.right,root,root.left的顺序,递归遍历,使得堆栈中最底部是最大的数,往上依次减小。
解答
The text was updated successfully, but these errors were encountered: