public BinaryTree(int[] post, int[] in) {
this.root = this.construct(post, 0, post.length - 1, in, 0, in.length - 1);
}
private Node construct(int[] post, int plo, int phi, int[] in, int ilo, int ihi) {
if (ilo > ihi || phi< plo) {
return null;
}
Node root = new Node();
root.data = post[phi];
int idx = -1;
for (int i = ilo; i <= ihi; i++) {
if (in[i] == root.data) {
idx = i;
break;
}
}
root.right = construct(post, plo,phi-1, in, idx + 1, ihi);
root.left = construct(post, plo,phi-2, in, ilo, idx - 1);
return root;
}
The problem is why does the program gets AC even though I pass ‘phi - 2’ as the argument for ‘plo’ for the left subtree. The ‘plo’ value will get changed to it’s initial value when the stack unfolds, still it works.