I was given a programming problem that given an array, determine if it is a post order traversal of a binary tree. My solution is as follows:
public static boolean isPostOrder(int[] array) {
int root = array[array.length - 1];
int i = 0;
while(array[i] < root) {
i++;
}
while(array[i] > root) {
i++;
}
return i == array.length - 1;
}
I am trying to understand Big O. I have read this tutorial:
What is a plain English explanation of "Big O" notation?
However, I am still confused about addition and the while loops. I'm assuming in this case my while loops are O(1) since we are just comparing a value in an array to an integer, or am I wrong about this?
Now the addition is also O(1) because we are just adding 1 to some integer 1, right?
Therefore, is this an O(1) solution or am I missing something?