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

Commit07d09a0

Browse files
add a solution for 173
1 parent76d2c52 commit07d09a0

File tree

1 file changed

+30
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+30
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
importcom.fishercoder.common.classes.TreeNode;
44

5+
importjava.util.Deque;
56
importjava.util.LinkedList;
67
importjava.util.Queue;
78
importjava.util.Stack;
@@ -74,4 +75,33 @@ public int next() {
7475
}
7576
}
7677
}
78+
79+
publicstaticclassSolution3 {
80+
/**
81+
* credit: https://leetcode.com/problems/binary-search-tree-iterator/discuss/52647/Nice-Comparison-(and-short-Solution
82+
*/
83+
publicstaticclassBSTIterator {
84+
Deque<TreeNode>stack;
85+
TreeNodevisit;
86+
87+
publicBSTIterator(TreeNoderoot) {
88+
stack =newLinkedList<>();
89+
visit =root;
90+
}
91+
92+
publicintnext() {
93+
while (visit !=null) {
94+
stack.addLast(visit);
95+
visit =visit.left;
96+
}
97+
TreeNodenext =stack.pollLast();
98+
visit =next.right;
99+
returnnext.val;
100+
}
101+
102+
publicbooleanhasNext() {
103+
returnvisit !=null && !stack.isEmpty();
104+
}
105+
}
106+
}
77107
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp