u.ranjith
BAN USERIf I am correct, we will be given a post order tree like below:
LLILLII or LLILLILLIII.
For this they have given a condition that a node can have 0 or 2 children compulsory.
Algo for constructing a BinaryTreeNode in Java Language :
BinaryTreeFromPostOrder(char[] A, int i){
if(A == null){
return null;
}
BinaryTreeNode new Node = new BinaryTreeNode();
newNode.setData(A[i]);
newNode.setLeft(null);
newNode.setRight(null);
if(A[i] == 'L'){
return newNode;
}
i = i - 1;
newNode.setRight(BuildTreeFromPostOrder(A,i));
i = i - 1;
newNode.setLeft(BuildTreeFromPostOrder(A,i));
return newNode;
}
Call this method with BinaryTreeFromPostOrder(A, A.length-1);
Let me know if I did any thing wrong or if I understood the question wrongly.
Thanks,
Ranjith
Thx
- u.ranjith August 05, 2013