/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
type BSTIterator struct {
data []int
}
func Constructor(root *TreeNode) BSTIterator {
iter := BSTIterator{data: []int{}}
iter.reverse(root)
return iter
}
func (this *BSTIterator) reverse(root *TreeNode) {
if root == nil {
return
}
this.reverse(root.Left)
this.data = append(this.data, root.Val)
this.reverse(root.Right)
}
func (this *BSTIterator) Next() int {
data := this.data[0]
this.data = this.data[1:]
return data
}
func (this *BSTIterator) HasNext() bool {
return len(this.data)> 0
}
/**
* Your BSTIterator object will be instantiated and called as such:
* obj := Constructor(root);
* param_1 := obj.Next();
* param_2 := obj.HasNext();
*/