I have just started studying Binary Tree. Is there an algorithm to find out the binary tree structure,given the Inorder and Postorder OR Inorder and Preorder? I have been trying to do it manually,but it never comes out correct.For eg.-These two are valid Inorder and Postorder traversal of a given tree:
Inorder: D B F E A G C L J H K Postorder : D F E B G L J K H C A
Clearly A is the root as it is the last element in Postorder. Now looking in Inorder,the left subtree becomes: {D B F E} and right subtree becomes: {G C L J H K}. The root of right subtree would be the second last element in preorder i.e C. I can now further divide the right subtree(with C as root), giving {G} as right subtree and {L J H K} as left. Therefore I have this structure:
A
\
C
/
G
But,whatever algorithm I apply,next seems to work differently for different trees . Someone please explain.
divide […subtree] with C as root), giving {G} as right subtree and {L J H K} as left
- this has the "labels" left and right inverted – greybeard