diff --git a/solution/0173.Binary Search Tree Iterator/Solution.java b/solution/0173.Binary Search Tree Iterator/Solution.java new file mode 100644 index 0000000000000..a251b9cb1f613 --- /dev/null +++ b/solution/0173.Binary Search Tree Iterator/Solution.java @@ -0,0 +1,50 @@ +public class Solution { + + /** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ + class BSTIterator { + + private TreeNode currentNode = null; + private Stack stack = new Stack(); + + public BSTIterator(TreeNode root) { + if (root != null) { + currentNode = root; + } + } + + /** @return the next smallest number */ + public int next() { + while (currentNode != null) { + stack.push(currentNode); + currentNode = currentNode.left; + } + + currentNode = stack.pop(); + TreeNode node = currentNode; + currentNode = currentNode.right; + + return node.val; + } + + /** @return whether we have a next smallest number */ + public boolean hasNext() { + return currentNode != null || !stack.isEmpty(); + } + } + + /** + * Your BSTIterator object will be instantiated and called as such: + * BSTIterator obj = new BSTIterator(root); + * int param_1 = obj.next(); + * boolean param_2 = obj.hasNext(); + */ + +}