-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTreeIterator.py
More file actions
38 lines (29 loc) · 931 Bytes
/
binarySearchTreeIterator.py
File metadata and controls
38 lines (29 loc) · 931 Bytes
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# Source: https://leetcode.com/problems/binary-search-tree-iterator/
# Author: Miao Zhang
# Date: 2021-01-24
# 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: TreeNode):
self.stack = []
self.inorder(root)
def inorder(self, root: TreeNode):
if not root:
return
self.inorder(root.left)
self.stack.append(root.val)
self.inorder(root.right)
def next(self) -> int:
return self.stack.pop(0)
def hasNext(self) -> bool:
return len(self.stack) != 0
# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()