-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidBST.java
More file actions
30 lines (28 loc) · 961 Bytes
/
ValidBST.java
File metadata and controls
30 lines (28 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.*;
public class Solution {
static boolean check(int []a, int n) {
Stack<Integer> stack = new Stack<Integer>();
int root = -1;
for(int i = 0;i < n; i++) {
if(a[i] < root)
return false;
while(!stack.isEmpty() && a[i] > stack.peek()) {
root = stack.peek();
stack.pop();
}
stack.push(a[i]);
}
return true;
}
public static void main(String[] args) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scan = new Scanner(System.in);
int q = scan.nextInt();
while(q --> 0) {
int n = scan.nextInt();
int []a = new int[n];
for(int i = 0; i < n; i++) a[i] = scan.nextInt();
System.out.println(check(a, n) == true ? "YES" : "NO");
}
}
}