Skip to content

Commit 90ac6d6

Browse files
refactor 255
1 parent 1707ff7 commit 90ac6d6

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)
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