There is a given array and we need to know if it represents a post-order traversal of BST. (example: if the question was in-order instead of post-order , we only need to check if the array is sorted time o(n))

- 51,337
- 7
- 54
- 119

- 31
- 1
- 4
-
Sounds like an interesting homework problem. What have you come up with in your attempts to solve it? – John Coleman Jan 10 '17 at 12:32
-
It could represent nearly *anything* (e.g.: my mom's favorite lottery numbers)... please clarify the question. – Karoly Horvath Jan 10 '17 at 12:37
-
@KarolyHorvath By "represents" I think that OP basically means "is". Can the array be obtained by starting with a binary search tree, doing a post-order traversal, and filling in the elements in the array by the values at the nodes in the order visited? I think that the question is clear enough, but shows no research effort. – John Coleman Jan 10 '17 at 12:48
-
if we make a post-order traversal on any BST , and we got array that represent a BST , but also it could represent any other tree , but at least ONE TREE must be a BST of all the trees that can be presented . – m.zmiro Jan 10 '17 at 12:51
-
@m.zmiro: That was my point, thank you... ambiguity resolved. – Karoly Horvath Jan 10 '17 at 13:06
-
If there is a BST that works, then it is unique. A hint for answering your question is -- find an algorithm that reconstructs the tree (if it exists). To be concrete, can you find the BST corresponding to `[1, 4, 7, 6, 3, 13, 14, 10, 8]`? Check your answer against the example tree towards the top of: https://en.wikipedia.org/wiki/Binary_search_tree – John Coleman Jan 10 '17 at 13:08
-
Possible duplicate of [How to construct BST given post-order traversal](http://stackoverflow.com/questions/13167536/how-to-construct-bst-given-post-order-traversal) – Jan 10 '17 at 15:20
-
@Paul I wouldn't say a duplicate since this question is about *testing* if a given array is a post-order traversal of a BST. It might be possible that this decision problem is easier than actually constructing the tree. After all, it is often easier to validate the input of a function than it is to run the function itself. I suspect that in this case it isn't, and that any algorithm which answers the decision problem at least implicitly constructs the tree (when such a tree exists). But in the absence of proof I think that the question should be left open. – John Coleman Jan 10 '17 at 16:45
-
i think i found an answer , correct me if iam wrong. in array that represent a post order traversal of BST , the root is the last element then after that the problem is to find the left child , because its a BST the left child is smaller than the root , so we start from the root and we go left in the array till we find the left child , the first element that its smaller than root is our left child , after we find him right of the left child is our right sub tree . – m.zmiro Jan 10 '17 at 17:29
-
from the left child to left is our left sub tree, how to check if its legal , there shouldnt be any element that is bigger than the root in the left sub tree (coz its BST) if we find something like that then it doesnt represent BST,if we didnt find any we send both of the subs to recursion . – m.zmiro Jan 10 '17 at 17:29
-
That is the idea -- but you need to be careful. If the tree is unbalanced then you might have a left child but no right child or vice versa. Note that if a right child exists then it is the second-to-last element in the list – John Coleman Jan 10 '17 at 17:57
-
yh , so i think the time complexity is O(N!) ->O(N^2) – m.zmiro Jan 10 '17 at 18:16
3 Answers
With the following code it can be estimated if the given array can possibly represent a BST or not.
PROCEDURE:
10
/ \
7 12
/ \ \
6 9 31
/
14
let the array be arr={6,9,7,14,31,12,10}
now we start traversing the array from the end till we reach the index with the element less than the end_index (arr[end_index]=10).And we store that that index in temp. (here temp=2 i.e. arr[temp]=7 as 7<10) we traverse again from temp till the start_index. if we find a number greater than 10(element of the last index), we return false.if not ,traverse till the start_index. now we split the array in two halves {6,9,7} and {14,31,12,10}. and follow the same procedure for both.
LOGIC:
post order means {{LEFT elements},{RIGHT elements},{ROOT}}. hence the order of array should be {(elements smaller than parent),(elements greater than parents),(parent) }
and this is what we make sure while traversing the array.
The code is as follows:
public class ckeckIfPostOrderAbinaryTree {
public static void main(String[] args) {
int[] arr={6,9,7,14,31,12,10};
System.out.println(solve(arr,0,arr.length-1));
}
public static boolean solve(int[] arr,int strt,int end){
if(strt>=end){
return true;
}
int x=arr[end],index=end-1;
while(index>=strt && arr[index]>x){
index--;
}
int temp=index;
while(index>=strt){
if(arr[index]>x){
return false;
}
index--;
}
if(temp==strt){
return solve(arr,strt,end-1);
}
else{
return (solve(arr,strt,temp) && solve(arr,temp+1,end-1));
}
}
}

- 2,422
- 7
- 33
- 40

- 41
- 3
-
This solution works great if we assume that there is no duplicates in the given array, but it doesn't work on array like {1,1,1,1,1,1}. So I suggest you to change this line of code `if(arr[index]>x)` to `if(arr[index]>=x)` from the second while loop block to make this solution works better. – kenshinji Apr 30 '20 at 01:43
Postorder traversal is done by {LEFT Subtree}{Right Subtree}{ROOT}. Hence the idea here is to start traversing the array from the right end. The last element in the array is the root, and from Right to left, when you encounter smaller element, that marks the boundary to start with the left subtree, BST won't have a larger element from this index, this applies for each subtree. Algorithm in C++ goes like this -
bool isBST(int arr[], int n){
//Set root to the max integer value.
int root = INT_MAX;
//Stack used to keep track of current subtree
std::stack<int>s;
//Start at the end of the array because Postorder traversal is
// <L><R><D>
for (int i = n-1; i>=0; i--) {
//False if any larger number in the left subtree
if (arr[i]> root) {
return false;
}
//Reset the root for boundary of every subtree,
//when current item is smaller than top,
//that means we are starting with left subtree of this root
while (!s.empty() && arr[i] < s.top()) {
root = s.top();
s.pop();
}
//Keep pushing the elements otherwise
s.push(arr[i]);
}
return true;
}

- 524
- 4
- 14
-
1
-
This is the optimal solution with O(n) complexity, using monotonic decreasing stack. – Zongjun Apr 13 '21 at 20:56
Quick sanity check: if the last item in the array isn't the root node then it's not the result of a post-order traversal. Similarly, the first item in an array is the root if a pre-order traversal was done.

- 272
- 1
- 6