从前序遍历和中序遍历构建二叉树
class Solution { public HashMapmap = new HashMap<>(); public TreeNode buildTree(int[] preorder, int[] inorder) { int len = preorder.length; for(int i=0;i end) { return null; } TreeNode root = new TreeNode(preorder[first]); int middle = map.get(preorder[first]); root.left = buildTree2(preorder,inorder,start,middle-1,first+1); root.right = buildTree2(preorder,inorder,middle+1,end,first+(middle-start)+1); return root; }}