Skip to content

Commit 87733ed

Browse files
refactor 173
1 parent 8ecb7de commit 87733ed

File tree

1 file changed

+60
-72
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+60
-72
lines changed

src/main/java/com/fishercoder/solutions/_173.java

Lines changed: 60 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -8,91 +8,79 @@
88

99
/**
1010
* 173. Binary Search Tree Iterator
11+
*
1112
* Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
12-
* <p>
13+
*
1314
* Calling next() will return the next smallest number in the BST.
14-
* <p>
15+
*
1516
* Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
1617
*/
1718
public class _173 {
1819

19-
public static class Solution1 {
20+
public static class Solution1 {
2021

21-
public static class BSTIterator {
22+
public static class BSTIterator {
23+
private Queue<Integer> queue;
2224

23-
private Queue<Integer> queue;
24-
25-
/**
26-
* My natural idea is to use a queue to hold all elements in the BST, traverse it while constructing the iterator, although
27-
* this guarantees O(1) hasNext() and next() time, but it uses O(n) memory.
28-
*/
29-
//Cheers! Made it AC'ed at first shot! Praise the Lord! Practice does make perfect!
30-
//I created a new class to do it using Stack to meet O(h) memory: {@link fishercoder.algorithms._173_using_stack}
31-
public BSTIterator(TreeNode root) {
32-
queue = new LinkedList<>();
33-
if (root != null) {
34-
dfs(root, queue);
35-
}
36-
}
37-
38-
private void dfs(TreeNode root, Queue<Integer> q) {
39-
if (root.left != null) {
40-
dfs(root.left, q);
41-
}
42-
q.offer(root.val);
43-
if (root.right != null) {
44-
dfs(root.right, q);
45-
}
46-
}
47-
48-
/**
49-
* @return whether we have a next smallest number
50-
*/
51-
public boolean hasNext() {
52-
return !queue.isEmpty();
53-
}
54-
55-
/**
56-
* @return the next smallest number
57-
*/
58-
public int next() {
59-
return queue.poll();
60-
}
25+
public BSTIterator(TreeNode root) {
26+
queue = new LinkedList<>();
27+
if (root != null) {
28+
dfs(root, queue);
6129
}
62-
}
63-
64-
public static class Solution2 {
65-
public static class BSTIterator {
66-
/**
67-
* This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we
68-
* push all its right nodes into the stack if there are any.
69-
* This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge
70-
* since h could be much smaller than n. Cheers!
71-
*/
30+
}
7231

73-
private Stack<TreeNode> stack;
32+
private void dfs(TreeNode root, Queue<Integer> q) {
33+
if (root.left != null) {
34+
dfs(root.left, q);
35+
}
36+
q.offer(root.val);
37+
if (root.right != null) {
38+
dfs(root.right, q);
39+
}
40+
}
7441

75-
public BSTIterator(TreeNode root) {
76-
stack = new Stack();
77-
pushToStack(root, stack);
78-
}
42+
public boolean hasNext() {
43+
return !queue.isEmpty();
44+
}
7945

80-
private void pushToStack(TreeNode root, Stack<TreeNode> stack) {
81-
while (root != null) {
82-
stack.push(root);
83-
root = root.left;
84-
}
85-
}
46+
public int next() {
47+
return queue.poll();
48+
}
49+
}
50+
}
51+
52+
public static class Solution2 {
53+
public static class BSTIterator {
54+
/**
55+
* This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we
56+
* push all its right nodes into the stack if there are any.
57+
* This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge
58+
* since h could be much smaller than n. Cheers!
59+
*/
60+
61+
private Stack<TreeNode> stack;
62+
63+
public BSTIterator(TreeNode root) {
64+
stack = new Stack();
65+
pushToStack(root, stack);
66+
}
67+
68+
private void pushToStack(TreeNode root, Stack<TreeNode> stack) {
69+
while (root != null) {
70+
stack.push(root);
71+
root = root.left;
72+
}
73+
}
8674

87-
public boolean hasNext() {
88-
return !stack.isEmpty();
89-
}
75+
public boolean hasNext() {
76+
return !stack.isEmpty();
77+
}
9078

91-
public int next() {
92-
TreeNode curr = stack.pop();
93-
pushToStack(curr.right, stack);
94-
return curr.val;
95-
}
96-
}
79+
public int next() {
80+
TreeNode curr = stack.pop();
81+
pushToStack(curr.right, stack);
82+
return curr.val;
83+
}
9784
}
85+
}
9886
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy