Skip to content

Commit 8d816af

Browse files
refactor 173
1 parent d92b2a7 commit 8d816af

File tree

1 file changed

+53
-62
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+53
-62
lines changed

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

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,81 +6,72 @@
66
import java.util.Queue;
77
import java.util.Stack;
88

9-
/**
10-
* 173. Binary Search Tree Iterator
11-
*
12-
* Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
13-
*
14-
* Calling next() will return the next smallest number in the BST.
15-
*
16-
* Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
17-
*/
189
public class _173 {
1910

20-
public static class Solution1 {
11+
public static class Solution1 {
2112

22-
public static class BSTIterator {
23-
private Queue<Integer> queue;
13+
public static class BSTIterator {
14+
private Queue<Integer> queue;
2415

25-
public BSTIterator(TreeNode root) {
26-
queue = new LinkedList<>();
27-
if (root != null) {
28-
dfs(root, queue);
29-
}
30-
}
16+
public BSTIterator(TreeNode root) {
17+
queue = new LinkedList<>();
18+
if (root != null) {
19+
dfs(root, queue);
20+
}
21+
}
3122

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-
}
23+
private void dfs(TreeNode root, Queue<Integer> q) {
24+
if (root.left != null) {
25+
dfs(root.left, q);
26+
}
27+
q.offer(root.val);
28+
if (root.right != null) {
29+
dfs(root.right, q);
30+
}
31+
}
4132

42-
public boolean hasNext() {
43-
return !queue.isEmpty();
44-
}
33+
public boolean hasNext() {
34+
return !queue.isEmpty();
35+
}
4536

46-
public int next() {
47-
return queue.poll();
48-
}
37+
public int next() {
38+
return queue.poll();
39+
}
40+
}
4941
}
50-
}
5142

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-
*/
43+
public static class Solution2 {
44+
public static class BSTIterator {
45+
/**
46+
* This is a super cool/clever idea: use a stack to store all the current left nodes of the BST, when pop(), we
47+
* push all its right nodes into the stack if there are any.
48+
* This way, we use only O(h) memory for this iterator, this is a huge saving when the tree is huge
49+
* since h could be much smaller than n. Cheers!
50+
*/
6051

61-
private Stack<TreeNode> stack;
52+
private Stack<TreeNode> stack;
6253

63-
public BSTIterator(TreeNode root) {
64-
stack = new Stack();
65-
pushToStack(root, stack);
66-
}
54+
public BSTIterator(TreeNode root) {
55+
stack = new Stack();
56+
pushToStack(root, stack);
57+
}
6758

68-
private void pushToStack(TreeNode root, Stack<TreeNode> stack) {
69-
while (root != null) {
70-
stack.push(root);
71-
root = root.left;
72-
}
73-
}
59+
private void pushToStack(TreeNode root, Stack<TreeNode> stack) {
60+
while (root != null) {
61+
stack.push(root);
62+
root = root.left;
63+
}
64+
}
7465

75-
public boolean hasNext() {
76-
return !stack.isEmpty();
77-
}
66+
public boolean hasNext() {
67+
return !stack.isEmpty();
68+
}
7869

79-
public int next() {
80-
TreeNode curr = stack.pop();
81-
pushToStack(curr.right, stack);
82-
return curr.val;
83-
}
70+
public int next() {
71+
TreeNode curr = stack.pop();
72+
pushToStack(curr.right, stack);
73+
return curr.val;
74+
}
75+
}
8476
}
85-
}
8677
}

0 commit comments

Comments
 (0)