This is the original title of Sword Finger Office and PTA. The code in the early years is as follows:
Now rewrite, the pursuit is no longer simply through and the best way of thinking, but strict code simplicity and efficiency. Compared with the previous simple recursion, the hash table is added to store the position of the precedent traversal elements in the middle traversal array, which greatly improves the speed of interval partition. The principle of post-order traversal + mid-order traversal - > construction of binary tree is the same.
Portal: Constructing Binary Trees from Pre-order and Mid-order Traversal Sequences
Given preorder and inorder traversal of a tree, construct the binary tree.
Note: You may assume that duplicates do not exist in the tree.
A binary tree is constructed according to the pre-order traversal and the middle-order traversal of a tree.
Note: You can assume that there are no duplicate elements in the tree.
For example, given Preorder traversal preorder = 3,9,20,15,7 Intermediate traversal inorder = 9,3,15,20,7 Returns the following binary tree: 3 / \ 9 20 / \ 15 7
Portal: Constructing Binary Trees from Ordered and Post-Ordered Traversal Sequences
Given inorder and postorder traversal of a tree, construct the binary tree.
Note: You may assume that duplicates do not exist in the tree.
A binary tree is constructed according to the mid-order traversal and post-order traversal of a tree.
Note: You can assume that there are no duplicate elements in the tree.
For example, given Intermediate traversal inorder = 9,3,15,20,7 Postorder traversal = 9,15,7,20,3] Returns the following binary tree: 3 / \ 9 20 / \ 15 7
import java.util.HashMap; /** * * Given preorder and inorder traversal of a tree, construct the binary tree. * You may assume that duplicates do not exist in the tree. * A binary tree is constructed according to the pre-order traversal and the middle-order traversal of a tree. * You can assume that there are no duplicate elements in the tree. * */ public class ConstructBinaryTreeFromPreorderAndInorderTraversal { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } //The hash table is used to store the location of each node, and then the tree is built recursively. int[] preOrder; int[] inOrder; int[] postOrder; HashMap<Integer, Integer> map = new HashMap<>(); //Pre-order traversal + mid-order traversal - > reconstruction of binary tree public TreeNode buildTree(int[] preorder, int[] inorder) { preOrder = preorder; inOrder = inOrder; int idx = 0; for(int value : inorder){ map.put(value, idx++); } return Solution(0, 0, inorder.length - 1); } public TreeNode Solution(int rootLocPre, int begin, int end){ if(rootLocPre >= preOrder.length || begin > end){ return null; } TreeNode root = new TreeNode(preOrder[rootLocPre]); if(begin != end){ int rootLocIn = map.get(preOrder[rootLocPre]); root.left = Solution(rootLocPre + 1, begin, rootLocIn - 1); root.right = Solution(rootLocPre + rootLocIn - begin + 1, rootLocIn + 1, end); } return root; } //Post-order traversal + middle-order traversal - > reconstruction of binary tree public TreeNode buildTree2(int[] inorder, int[] postorder) { postOrder = postorder; inOrder = inOrder; int idx = 0; for(int value : inorder){ map.put(value, idx++); } return Solution2(postorder.length - 1, 0, inorder.length - 1); } public TreeNode Solution2(int rootLocPost, int begin, int end){ if(rootLocPost < 0 || begin > end){ return null; } TreeNode root = new TreeNode(postOrder[rootLocPost]); if(begin != end){ int rootLocIn = map.get(postOrder[rootLocPost]); root.left = Solution2(rootLocPost - (end - rootLocIn + 1), begin, rootLocIn - 1); root.right = Solution2(rootLocPost - 1, rootLocIn + 1, end); } return root; } }
# Coding for an hour, Copying for a second. Leave a message of praise. Thank you.#