-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path173.py
More file actions
39 lines (32 loc) · 1.18 KB
/
173.py
File metadata and controls
39 lines (32 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 這題是要做出 tree 的 in-order search,然後去設計初始化函式、next value 函式跟 次一數值存在與否函式
# 做法就是在初始化的時候先做一次 inorder search,然後再記錄一下現在的 index 在哪邊來決定有沒有下一個數值
# 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 = right
class BSTIterator:
def __init__(self, root: Optional[TreeNode]):
self.res = []
def traversal(root):
if root is None:
return
traversal(root.left)
self.res.append(root.val)
traversal(root.right)
traversal(root)
self.idx = 0
def next(self) -> int:
if self.idx < len(self.res):
v = self.res[self.idx]
self.idx += 1
return v
def hasNext(self) -> bool:
if self.idx < len(self.res):
return True
return False
# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()