Skip to content

Instantly share code, notes, and snippets.

@fanzhang312
Created December 3, 2014 22:22
Show Gist options
  • Save fanzhang312/1e42605bfce4ec304c09 to your computer and use it in GitHub Desktop.
Save fanzhang312/1e42605bfce4ec304c09 to your computer and use it in GitHub Desktop.
Preorder Traversal BST
public static boolean validBST(int[] arr){
int lowerbound = Integer.MIN_VALUE;
Stack<Integer> stack = new Stack<Integer>();
for(int i : arr){
if(i < lowerbound) return false;
while(!stack.isEmpty() && i > stack.peek()){
lowerbound = stack.pop();
}
stack.push(i);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment