File tree Expand file tree Collapse file tree 1 file changed +25
-5
lines changed
Algorithms/0173.binary-search-tree-iterator Expand file tree Collapse file tree 1 file changed +25
-5
lines changed Original file line number Diff line number Diff line change @@ -14,22 +14,42 @@ type TreeNode = kit.TreeNode
1414
1515// BSTIterator is the iterator of BST
1616type BSTIterator struct {
17+ index * int
18+ nums * []int
1719}
1820
1921// Constructor returns a BST iterator
2022func Constructor (root * TreeNode ) BSTIterator {
21-
22- return BSTIterator {}
23+ nums := convert (root )
24+ index := 0
25+ return BSTIterator {
26+ index : & index ,
27+ nums : & nums ,
28+ }
2329}
2430
2531// Next returns the next smallest number
2632func (it * BSTIterator ) Next () int {
27-
28- return 0
33+ res := (* it .nums )[* it .index ]
34+ * it .index ++
35+ return res
2936}
3037
3138// HasNext returns whether we have a next smallest number
3239func (it * BSTIterator ) HasNext () bool {
40+ return * it .index < len (* it .nums )
41+ }
42+
43+ func convert (root * TreeNode ) []int {
44+ res := make ([]int , 0 , 128 )
45+ return res
46+ }
3347
34- return false
48+ func helper (root * TreeNode , res * []int ) {
49+ if root == nil {
50+ return
51+ }
52+ helper (root .Left , res )
53+ * res = append (* res , root .Val )
54+ helper (root .Right , res )
3555}
You can’t perform that action at this time.
0 commit comments