Created
December 3, 2014 22:22
-
-
Save fanzhang312/1e42605bfce4ec304c09 to your computer and use it in GitHub Desktop.
Preorder Traversal BST
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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