Friday 15 August 2014

algorithm - Create binary tree from inorder and post order traversal -


I was trying to familarize with the question of creating inorder and postorder transcellal trees. I have written the following code, but something is going wrong that I did not know. Can anyone help me on this?

sample i / p:

in int [] = {4,10,3,1,7,11,8,2}; Int post [] = {4,1,3,10,11,8,2,7}; Public static tree node build posters (int post [], int n, in offset, map and lieutenant integer; Return null; Int rootwall = post [N-1]; Int i = (indexMap.get (rootVal) - offset); Treeoid Root = New Treeode (RootWall); Root.setLeft (buildInorderPostorder (Post, I, Offset, IndexMap, I-Offset)); Root.setRight (buildInorderPostorder (Post, N-1, Offset + Eye, IndexMap, N-1I)); Return route; }

root.setRight seems incorrect. Should not be offset + i , it should be offset + i + 1 :

  root.setRight (buildInorderPostorder (post, n - 1, offset + i + 1, indexmap, n-1-i));    

No comments:

Post a Comment