1. Description of the problem
To enter the results of the pre-order traversal and the middle sequence traversal of a binary tree, rebuild the two-fork tree. Assume that no duplicate numbers are included in the result of the input's pre-order traversal and the middle-order traversal. For example, enter the pre-sequence traversal sequence {1,2,4,7,3,5,6,8} and the middle sequence traversal sequence {4,7,2,1,5,3,8,6}, then rebuild the binary tree and return.
2. Problem analysis
2.1 First understand the structure of the binary tree
2.2 Understanding of the three traversal sequences of binary trees (pre-sequence traversal, mid-sequence traversal, and post-order traversal)
Pre-sequence traversal : Middle and left
Middle Sequence traversal : left- Middle -right
post-traversal : left right
according to pre-sequence traversal sequence and middle sequence traversal sequence , or after sequence traversal sequence and middle sequence traversal sequence, which can uniquely determine a two-fork tree.
2.3 Iterations of programming ideas
3. Source code
PackageWww.nowcoder.com.conquerOffer.binaryTree;Importjava.util.Arrays;/*** Rebuilding the binary tree * Enter the results of the pre-sequence traversal and the middle sequence traversal of a binary tree, and rebuild the two-fork tree. Assume that no duplicate numbers are included in the result of the input's pre-order traversal and the middle-order traversal. For example, enter the pre-sequence traversal sequence {1,2,4,7,3,5,6,8} and the middle sequence traversal sequence {4,7,2,1,5,3,8,6}, then rebuild the binary tree and return. * http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking *@authorSunny **/ Public classBinarytreerebuild {/*** Rebuilding the binary tree *@paramPre-sequence traversal sequence array *@paramin middle order traversal sequence Array *@returntwo fork root node*/ PublicTreeNode Reconstructbinarytree (int[] Pre,int[] in) { //The first step: Verifying whether the sequence traversal sequence or the middle order traversal array is empty if(NULL= = Pre | |NULL= = In | | Pre.length <= 0 | | In.length <= 0) return NULL; //Step Two: Get the value of the root node from the previous sequence traversal sequence intRootval = pre[0]; //Step Three: Set the value of the root nodeTreeNode RootNode =NewTreeNode (Rootval); //Fourth step: Get the index value of the root node in the middle sequence traversal sequence intRootindex = Findinarray (in, rootval);//binary search algorithm with the array tool class//The fifth step: Get the sequence of the left subtree, the sequence of the right subtree, the middle sequence traversal sequence of the Zuozi, the middle sequence traversal sequence of the right sub-tree.//Zuozi Sequential traversal sequence of the first order int[] Preleft = Arrays.copyofrange (pre, 1, rootindex+1); //pre-sequence traversal sequence of right subtree int[] Preright = Arrays.copyofrange (Pre, rootindex+1, pre.length); //Zuozi sequential traversal sequence int[] Inleft = Arrays.copyofrange (in, 0, Rootindex); //The Middle sequence traversal sequence of right sub-tree int[] Inright = Arrays.copyofrange (In, rootindex+1, pre.length); Binarytreerebuild Binarytreerebuild=NewBinarytreerebuild (); //Sixth step: Construct the Saozi right sub-treeTreeNode Lefttree =Binarytreerebuild.reconstructbinarytree (Preleft, inleft); TreeNode Righttree=Binarytreerebuild.reconstructbinarytree (Preright, inright); //Seventh Step: Set the Saozi right subtree of the root nodeRootnode.setleft (Lefttree); Rootnode.setright (Righttree); returnRootNode; } /*** Gets the subscript of the target in the array *@paramArr Array *@paramTarget found by *@return */ Private Static intFindinarray (int[] arr,inttarget) { //array is empty if(NULL= = Arr | | Arr.length <= 0) return-1; for(inti = 0; i < arr.length; i++){ if(target = =Arr[i])returni; } return-1; } Public Static voidMain (string[] args) {//pre-sequence traversal sequence int[] Pre =New int[]{1,2,4,7,3,5,6,8}; //Middle sequence traversal sequence int[] in =New int[]{4,7,2,1,5,3,8,6}; Binarytreerebuild Binarytreerebuild=NewBinarytreerebuild (); TreeNode RootNode=Binarytreerebuild.reconstructbinarytree (pre, in); System.out.println (RootNode); }}/*** Binary tree node *@authorSunny **/classTreeNode {intVal//Node ValueTreeNode left;//left dial hand treeTreeNode right;//Right sub-treeTreeNode (intX) {val = x;}//constructor Function//the Set method of Zuozi Public voidSetleft (TreeNode left) { This. left =Left ; } //set method for right subtree Public voidSetright (TreeNode right) { This. right =Right ; }}
4. Operation effect
1 [email protected]
Run effect
Rebuilding binary Tree-New Ket Net-Sword Point offer