Skip to content

Commit fa841d2

Browse files
committed
173 wrong answer
1 parent 105142d commit fa841d2

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

Algorithms/0173.binary-search-tree-iterator/binary-search-tree-iterator.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,42 @@ type TreeNode = kit.TreeNode
1414

1515
// BSTIterator is the iterator of BST
1616
type BSTIterator struct {
17+
index *int
18+
nums *[]int
1719
}
1820

1921
// Constructor returns a BST iterator
2022
func 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
2632
func (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
3239
func (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
}

0 commit comments

Comments
 (0)