Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created September 3, 2017 00:04
Show Gist options
  • Save cixuuz/8b2352e26a80b7da6736ccc99f03408f to your computer and use it in GitHub Desktop.
Save cixuuz/8b2352e26a80b7da6736ccc99f03408f to your computer and use it in GitHub Desktop.
[255. Verify Preorder Sequence in Binary Search Tree] #leetcode
class Solution {
// O(n) O(1)
public boolean verifyPreorder(int[] preorder) {
int low = Integer.MIN_VALUE, i = -1;
for (int p : preorder) {
if (p < low)
return false;
while (i >= 0 && p > preorder[i])
low = preorder[i--];
preorder[++i] = p;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment