File tree Expand file tree Collapse file tree 1 file changed +13
-19
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +13
-19
lines changed Original file line number Diff line number Diff line change 2
2
3
3
import java .util .Stack ;
4
4
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
- */
13
5
public class _255 {
14
6
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 );
24
19
}
25
- stack . push ( p ) ;
20
+ return true ;
26
21
}
27
- return true ;
28
22
}
29
23
30
24
}
You can’t perform that action at this time.
0 commit comments