"105-construct Binary tree from preorder and inorder traversal (constructed two-forked trees by sequence and sequence traversal)"
" leetcode-interview algorithm classic-java Implementation" "All topic Directory Index"
Original title
Given Preorder and inorder traversal of a, construct the binary tree.
Note:
You could assume that duplicates does not exist into the tree.
The main effect of the topic
A binary tree is constructed by giving a sequence of pre and sequence traversal.
Note:
-element repeating element in two-fork tree
ideas for solving problems
Sequence traversal The first element is the root node (k), in the middle sequence traversal sequence in the search for the value of K subscript idx,idx will be the sequence of traversal sequences into the left and right subtree, the same for the sequence traversal, can be recursive operation
Code Implementation
Tree Node class
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode (int x) {val = x;}
}
Algorithm implementation Class One:
public class Solution {public TreeNode buildtree (int[] preorder, int[] inorder) {if (preorder = NULL | | pre
Order.length ==0) {return null;
} hashmap<integer, integer> inordermap = new Hashmap<integer, integer> ();
for (int i=0;i<inorder.length;i++) {inordermap.put (inorder[i],i);
} deque<treenode> stack = new linkedlist<treenode> ();
TreeNode root = new TreeNode (preorder[0]);
Stack.push (root);
for (int i=1;i<preorder.length;i++) {TreeNode top = Stack.peek ();
int indextop = Inordermap.get (top.val);
int indexval = Inordermap.get (Preorder[i]);
TreeNode node = new TreeNode (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);
return root; }
}
Algorithm implementation Class two: (timeout)
public class Solution {public TreeNode buildtree (int[] preorder, int[] inorder) {//Parameter check if (preor Der = = NULL | | Inorder = = NULL | | Preorder.length = 0 | |
Preorder.length!= inorder.length) {return null;
return solve (preorder, 0, preorder.length-1, inorder, 0, inorder.length-1); /** * Build binary tree, the correctness of data input is guaranteed by the input data * * @param preorder First-order traversal results * @param x First-order traversal start position * @p
Aram Y-First-order traversal end position * @param the result of sequence traversal in Inorder * @param the start position of the sequence traversal in the I-@param j the end position of the sequence traversal
* @return The root node of the binary tree/public TreeNode solve (int[] preorder, int x, int y, int[] inorder, int i, int j) { if (x >= 0 && x <= y && i >= 0 && i <= j) {//Only one element if (
x = = y) {return new TreeNode (Preorder[x]);
else if (x < y) {//Record root node index int idx = i; while (idx <= J && Inorder[idx]!= preorder[x]) {idx++;
//Create root node TreeNode root = new TreeNode (Inorder[idx]);
The number of Zuozi nodes int leftlength = idx-i; if (Leftlength > 0) {//x + 1, x + leftlength: Zoozi tree start and end position ro
Ot.left = Solve (preorder, x + 1, x + leftlength, inorder, I, idx-1);
The number of nodes in the//right subtree is int rightlength = J-IDX; if (Rightlength > 0) {//x + leftlength + 1, y: Right subtree start and end position root.right = Solve P
Reorder, X + leftlength + 1, y, inorder, idx + 1, j);
return root;
} return null; }
}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, released in a new window to view the full picture.
Special Notes Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/47371985"