From 235c081fc40eaabc936075150a76f4197cf114e2 Mon Sep 17 00:00:00 2001 From: toeii Date: Thu, 11 Jul 2019 17:08:03 +0800 Subject: [PATCH 1/2] leetcode 0173 java code --- .../Solution.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 solution/0173.Binary Search Tree Iterator/Solution.java 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..f5cb244742915 --- /dev/null +++ b/solution/0173.Binary Search Tree Iterator/Solution.java @@ -0,0 +1,29 @@ +public class Solution { + + private TreeNode currentNode = null; + private Stack stack = new Stack(); + + public binarySearchTreeIterator(TreeNode root) { + if (root != null) { + currentNode = root; + } + } + + public boolean hasNext() { + return currentNode != null || !stack.isEmpty(); + } + + public TreeNode next() { + while (currentNode != null) { + stack.push(currentNode); + currentNode = currentNode.left; + } + + currentNode = stack.pop(); + TreeNode node = currentNode; + currentNode = currentNode.right; + + return node; + } + +} \ No newline at end of file From e1f0db465ca1e357602b680637439bb97808603d Mon Sep 17 00:00:00 2001 From: Toeii Date: Thu, 11 Jul 2019 22:11:33 +0800 Subject: [PATCH 2/2] Update Solution.java --- .../Solution.java | 65 +++++++++++++------ 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/solution/0173.Binary Search Tree Iterator/Solution.java b/solution/0173.Binary Search Tree Iterator/Solution.java index f5cb244742915..26a33cffe1848 100644 --- a/solution/0173.Binary Search Tree Iterator/Solution.java +++ b/solution/0173.Binary Search Tree Iterator/Solution.java @@ -1,29 +1,56 @@ public class Solution { - - private TreeNode currentNode = null; - private Stack stack = new Stack(); - public binarySearchTreeIterator(TreeNode root) { - if (root != null) { - currentNode = root; - } - } + /** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ - public boolean hasNext() { - return currentNode != null || !stack.isEmpty(); + class TreeNode { + int val; + TreeNode left; + TreeNode right; + TreeNode(int x) { val = x; } } - public TreeNode next() { - while (currentNode != null) { - stack.push(currentNode); - currentNode = currentNode.left; + class BSTIterator { + + Stack stack = new Stack<>(); + + public BSTIterator(TreeNode root) { + while (root!=null){ + stack.add(root); + root=root.left; + } } - currentNode = stack.pop(); - TreeNode node = currentNode; - currentNode = currentNode.right; + /** @return whether we have a next smallest number */ + public boolean hasNext() { + return !stack.isEmpty(); + } - return node; + /** @return the next smallest number */ + public int next() { + TreeNode current = stack.pop(); + TreeNode res = current; + current =current.right; + while (current!=null){ + stack.push(current); + current = current.left; + } + return res.val; + } } + + /** + * 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(); + */ -} \ No newline at end of file +}