"106-construct binary tree from Inorder and postorder traversal (construct two-fork trees via middle order and post-sequence traversal)"
"leetcode-Interview algorithm classic-java Implementation" "All Topics folder Index"
Original Question
Given Inorder and Postorder traversal of a tree, construct the binary tree.
Note:
Assume that duplicates does not exist in the tree.
Main Topic
A binary tree is constructed, given a sequence traversal and sequential traversal sequences.
Note:
There are no repeating elements in the tree
Thinking of solving problems
The last element of the post-order traversal is the root node (value R) of the tree, where the value of R is found in the sequential traversal sequence IDX,IDX the sequence traversal sequences into the left and right two subtrees, which can be divided into two sub-trees by the sequential traversal sequence. It is solved by recursion.
Code Implementation
publicclass TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}
Algorithm implementation class
Public class solution { PublicTreeNodeBuildtree(int[] inorder,int[] postorder) {//test of the parameters if(Inorder = =NULL|| Postorder = =NULL|| Inorder.length = =0|| Inorder.length! = postorder.length) {return NULL; }//Build a binary tree returnSolve (Inorder,0, Inorder.length-1, Postorder,0, Postorder.length-1); }/** * Build binary Tree * * @param inorder Results of sequential traversal * @param x start position of the sequence traversal * @par the end position of the sequence traversal in am y * @param postorder Results of post-order traversal * @param The start position of the sequential traversal * @p Aram J post-traversal end position * @return two fork tree * / PublicTreeNodeSolve(int[] inorder,intXintYint[] Postorder,intIintj) {if(x >=0&& x <= y && i >=0&& I <= j) {//There is only one element, (at this time there is also i=j) if(x = = y) {return NewTreeNode (Postorder[j]); }//More than one element. There are also i<j at this time Else if(x < y) {//Create root nodeTreeNode root =NewTreeNode (Postorder[j]);//Find root node subscript in middle sequence traversal intIDX = x; while(Idx < y && inorder[idx]! = Postorder[j]) {idx++; }//Left dial hand tree non-empty, build left sub-tree intLeftlength = Idx-x;if(Leftlength >0) {//I, I + leftLength-1, start of the left subtree of the pre-sequence traversal, end positionRoot.left = Solve (inorder, X, IDX-1, Postorder, I, i + leftlength-1); }//Right subtree non-empty, build right sub-tree intRightlength = Y-idx;if(Rightlength >0) {//i + Leftlength, j-1, start of the right subtree of the pre-sequence traversal, end positionRoot.right = Solve (inorder, IDX +1, Y, Postorder, i + leftlength, J-1); }returnRoot }Else{return NULL; } }return NULL; }}
Assessment Results
Click on the picture, the mouse does not release. Drag a position to view the full picture in the new form after you release it.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47371993"
"Leetcode-Interview algorithm classic-java implementation" "106-construct binary tree from Inorder and postorder traversal (construction of two Fork Trees II)"