public class SolutionTree { public TreeNode buildTree(char[] inorder, char[] postorder) { int inStart = 0; int inEnd = inorder.length-1; int postStart =0; int postEnd = postorder.length-1; return buildTree(inorder, inStart, inEnd, postorder, postStart, postEnd); } public TreeNode buildTree(char[] inorder, int inStart, int inEnd, char[] postorder, int postStart, int postEnd){ if(inStart > inEnd || postStart > postEnd) return null; char rootValue = postorder[postEnd]; //if(!isOperator(rootValue)) return null; TreeNode root = new TreeNode(rootValue); int k=0; for(int i=inStart; i< inorder.length; i++){ if(inorder[i]==rootValue){ //if(inorder[i]=='~') continue; k = i; break; } } root.left = buildTree(inorder, inStart, k-1, postorder, postStart, postStart+k-(inStart+1)); // Becuase k is not the length, it it need to -(inStart+1) to get the length root.right = buildTree(inorder, k+1, inEnd, postorder, postStart+k-inStart, postEnd-1); // postStart+k-inStart = postStart+k-(inStart+1) +1 return root; } }
Friday, May 15, 2015
Construct Tree From Inorder and Postorder in Java - Source Code
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment