Skip to content

Commit d0cc320

Browse files
refactor 255
1 parent 1f1e277 commit d0cc320

File tree

1 file changed

+13
-19
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+13
-19
lines changed

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

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,23 @@
22

33
import java.util.Stack;
44

5-
/**
6-
* 255. Verify Preorder Sequence in Binary Search Tree
7-
* Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.
8-
* You may assume each number in the sequence is unique.
9-
10-
Follow up:
11-
Could you do it using only constant space complexity?
12-
*/
135
public class _255 {
146

15-
public boolean verifyPreorder(int[] preorder) {
16-
int low = Integer.MIN_VALUE;
17-
Stack<Integer> stack = new Stack();
18-
for (int p : preorder) {
19-
if (p < low) {
20-
return false;
21-
}
22-
while (!stack.empty() && p > stack.peek()) {
23-
low = stack.pop();
7+
public static class Solution1 {
8+
public boolean verifyPreorder(int[] preorder) {
9+
int low = Integer.MIN_VALUE;
10+
Stack<Integer> stack = new Stack();
11+
for (int p : preorder) {
12+
if (p < low) {
13+
return false;
14+
}
15+
while (!stack.empty() && p > stack.peek()) {
16+
low = stack.pop();
17+
}
18+
stack.push(p);
2419
}
25-
stack.push(p);
20+
return true;
2621
}
27-
return true;
2822
}
2923

3024
}

0 commit comments

Comments
 (0)