"105-construct binary tree from preorder and inorder traversal (construction of two-fork trees via pre-order and middle-order traversal)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
Original Question
Given Preorder and inorder traversal of a tree, construct the binary tree.
Note:
Assume that duplicates does not exist in the tree.
Main Topic
Given a sequence of pre-order and sequence traversal, construct a binary tree
Note:
-element repeating element in two-fork tree
Thinking of solving problems
The first element of the pre-sequence traversal is the root node (k), in which the subscript idx,idx with K in the middle sequence traversal sequence divides the middle sequence traversal sequence into the left and right subtree, and the same for the pre-sequence traversal sequence, the recursive operation can be performed.
Code Implementation
Tree Node class
publicclass TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}
Algorithm implementation Class One:
Public classSolution { PublicTreeNodeBuildtree(int[] Preorder,int[] inorder) {if(Preorder = =NULL|| Preorder.length = =0){return NULL; } hashmap<integer, integer> inordermap =NewHashmap<integer, integer> (); for(intI=0; i<inorder.length;i++) {inordermap.put (inorder[i],i); } deque<treenode> stack =NewLinkedlist<treenode> (); TreeNode root =NewTreeNode (preorder[0]); Stack.push (root); for(intI=1; i<preorder.length;i++) {TreeNode top = Stack.peek ();intIndextop = Inordermap.Get(Top.val);intIndexval = Inordermap.Get(Preorder[i]); TreeNode node =NewTreeNode (Preorder[i]);if(Indexval<indextop) {Top.left=node; }Else{ while(Indexval>indextop) {top = Stack.pop (); Indextop = Stack.isempty ()? Integer.MAX_VALUE:inorderMap.Get(Stack.peek (). val); } top.right = node; } stack.push (node); }returnRoot }}
Algorithm implementation Class Two:(will time out)
Public class solution { PublicTreeNodeBuildtree(int[] Preorder,int[] inorder) {//Parameter check if(Preorder = =NULL|| Inorder = =NULL|| Preorder.length = =0|| Preorder.length! = inorder.length) {return NULL; }returnSolve (preorder,0, Preorder.length-1, Inorder,0, Inorder.length-1); }/** * Building a binary tree, the correctness of the data input is guaranteed by the input data itself * * @param Preorder result of the first order traversal * @param x start bit of first order traversal Place * @param The end position of the first sequence traversal * @param The result of the inorder sequence traversal * @param The start position of the sequence traversal * @param The end position of the middle sequence traversal * @return two fork tree root node * * PublicTreeNodeSolve(int[] Preorder,intXintYint[] inorder,intIintj) {if(x >=0&& x <= y && i >=0&& I <= j) {//Only one element if(x = = y) {return NewTreeNode (Preorder[x]); }Else if(x < y) {//Record the index of the root node. intidx = i; while(idx <= J && inorder[idx]! = Preorder[x]) {idx++; }//Create root nodeTreeNode root =NewTreeNode (Inorder[idx]);//Zuozi number of nodes intLeftlength = Idx-i;// if(Leftlength >0) {//x + 1, x + leftlength: Left dial hand tree start and end positionsRoot.left = Solve (preorder, X +1, X + leftlength, inorder, I, IDX-1); }number of nodes in//right sub-tree intRightlength = J-idx;if(Rightlength >0) {//x + leftlength + 1, y: Right sub-tree start and end positionsRoot.right = Solve (preorder, X + Leftlength +1, Y, inorder, idx +1, j); }returnRoot } }return NULL; }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47371985"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java implementation" "105-construct binary tree from preorder and inorder traversal (construction of two fork trees)"