Created
September 3, 2017 00:04
-
-
Save cixuuz/8b2352e26a80b7da6736ccc99f03408f to your computer and use it in GitHub Desktop.
[255. Verify Preorder Sequence in Binary Search Tree] #leetcode
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
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