# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclassBSTIterator:def__init__(self,root:TreeNode):definorder(root):ifroot:inorder(root.left)self.vals.append(root.val)inorder(root.right)self.cur=0self.vals=[]inorder(root)defnext(self)->int:res=self.vals[self.cur]self.cur+=1returnresdefhasNext(self)->bool:returnself.cur<len(self.vals)# Your BSTIterator object will be instantiated and called as such:# obj = BSTIterator(root)# param_1 = obj.next()# param_2 = obj.hasNext()
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */typeBSTIteratorstruct{stack[]*TreeNode}funcConstructor(root*TreeNode)BSTIterator{varstack[]*TreeNodefor;root!=nil;root=root.Left{stack=append(stack,root)}returnBSTIterator{stack:stack,}}func(this*BSTIterator)Next()int{cur:=this.stack[len(this.stack)-1]this.stack=this.stack[:len(this.stack)-1]fornode:=cur.Right;node!=nil;node=node.Left{this.stack=append(this.stack,node)}returncur.Val}func(this*BSTIterator)HasNext()bool{returnlen(this.stack)>0}/** * Your BSTIterator object will be instantiated and called as such: * obj := Constructor(root); * param_1 := obj.Next(); * param_2 := obj.HasNext(); */
// Definition for a binary tree node.// #[derive(Debug, PartialEq, Eq)]// pub struct TreeNode {// pub val: i32,// pub left: Option<Rc<RefCell<TreeNode>>>,// pub right: Option<Rc<RefCell<TreeNode>>>,// }//// impl TreeNode {// #[inline]// pub fn new(val: i32) -> Self {// TreeNode {// val,// left: None,// right: None// }// }// }usestd::rc::Rc;usestd::cell::RefCell;structBSTIterator{stack:Vec<i32>,}/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implBSTIterator{fndfs(root:&Option<Rc<RefCell<TreeNode>>>,stack:&mutVec<i32>){letnode=root.as_ref().unwrap().borrow();ifnode.right.is_some(){Self::dfs(&node.right,stack);}stack.push(node.val);ifnode.left.is_some(){Self::dfs(&node.left,stack);}}fnnew(root:Option<Rc<RefCell<TreeNode>>>)->Self{letmutstack=Vec::new();Self::dfs(&root,&mutstack);Self{stack,}}fnnext(&mutself)->i32{self.stack.pop().unwrap()}fnhas_next(&self)->bool{!self.stack.is_empty()}}/** * Your BSTIterator object will be instantiated and called as such: * let obj = BSTIterator::new(root); * let ret_1: i32 = obj.next(); * let ret_2: bool = obj.has_next(); */
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclassBSTIterator:def__init__(self,root:TreeNode):self.stack=[]whileroot:self.stack.append(root)root=root.leftdefnext(self)->int:cur=self.stack.pop()node=cur.rightwhilenode:self.stack.append(node)node=node.leftreturncur.valdefhasNext(self)->bool:returnlen(self.stack)>0# Your BSTIterator object will be instantiated and called as such:# obj = BSTIterator(root)# param_1 = obj.next()# param_2 = obj.hasNext()