# 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// }// }// }structBSTIterator{vals:Vec<i32>,index:usize,}usestd::rc::Rc;usestd::cell::RefCell;/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implBSTIterator{fninorder(root:&Option<Rc<RefCell<TreeNode>>>,res:&mutVec<i32>){ifletSome(node)=root{letnode=node.as_ref().borrow();Self::inorder(&node.left,res);res.push(node.val);Self::inorder(&node.right,res);}}fnnew(root:Option<Rc<RefCell<TreeNode>>>)->Self{letmutvals=vec![];Self::inorder(&root,&mutvals);BSTIterator{vals,index:0,}}fnnext(&mutself)->i32{self.index+=1;self.vals[self.index-1]}fnhas_next(&self)->bool{self.index!=self.vals.len()}}/** * 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()
// 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// }// }// }structBSTIterator{stack:Vec<Option<Rc<RefCell<TreeNode>>>>,}usestd::rc::Rc;usestd::cell::RefCell;/** * `&self` means the method takes an immutable reference. * If you need a mutable reference, change it to `&mut self` instead. */implBSTIterator{fndfs(mutroot:Option<Rc<RefCell<TreeNode>>>,stack:&mutVec<Option<Rc<RefCell<TreeNode>>>>){ifroot.is_some(){letleft=root.as_mut().unwrap().borrow_mut().left.take();stack.push(root);Self::dfs(left,stack);}}fnnew(root:Option<Rc<RefCell<TreeNode>>>)->Self{letmutstack=vec![];Self::dfs(root,&mutstack);BSTIterator{stack}}fnnext(&mutself)->i32{letnode=self.stack.pop().unwrap().unwrap();letmutnode=node.borrow_mut();ifnode.right.is_some(){Self::dfs(node.right.take(),&mutself.stack);}node.val}fnhas_next(&self)->bool{self.stack.len()!=0}}/** * 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(); */