Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8d816af

Browse files
refactor 173
1 parentd92b2a7 commit8d816af

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
importjava.util.Queue;
77
importjava.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
publicclass_173 {
1910

20-
publicstaticclassSolution1 {
11+
publicstaticclassSolution1 {
2112

22-
publicstaticclassBSTIterator {
23-
privateQueue<Integer>queue;
13+
publicstaticclassBSTIterator {
14+
privateQueue<Integer>queue;
2415

25-
publicBSTIterator(TreeNoderoot) {
26-
queue =newLinkedList<>();
27-
if (root !=null) {
28-
dfs(root,queue);
29-
}
30-
}
16+
publicBSTIterator(TreeNoderoot) {
17+
queue =newLinkedList<>();
18+
if (root !=null) {
19+
dfs(root,queue);
20+
}
21+
}
3122

32-
privatevoiddfs(TreeNoderoot,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+
privatevoiddfs(TreeNoderoot,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-
publicbooleanhasNext() {
43-
return !queue.isEmpty();
44-
}
33+
publicbooleanhasNext() {
34+
return !queue.isEmpty();
35+
}
4536

46-
publicintnext() {
47-
returnqueue.poll();
48-
}
37+
publicintnext() {
38+
returnqueue.poll();
39+
}
40+
}
4941
}
50-
}
5142

52-
publicstaticclassSolution2 {
53-
publicstaticclassBSTIterator {
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+
publicstaticclassSolution2 {
44+
publicstaticclassBSTIterator {
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-
privateStack<TreeNode>stack;
52+
privateStack<TreeNode>stack;
6253

63-
publicBSTIterator(TreeNoderoot) {
64-
stack =newStack();
65-
pushToStack(root,stack);
66-
}
54+
publicBSTIterator(TreeNoderoot) {
55+
stack =newStack();
56+
pushToStack(root,stack);
57+
}
6758

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

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

79-
publicintnext() {
80-
TreeNodecurr =stack.pop();
81-
pushToStack(curr.right,stack);
82-
returncurr.val;
83-
}
70+
publicintnext() {
71+
TreeNodecurr =stack.pop();
72+
pushToStack(curr.right,stack);
73+
returncurr.val;
74+
}
75+
}
8476
}
85-
}
8677
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp