public TreeNode buildTree(int[] preorder, int[] inorder) {
if (preorder == null || inorder == null) {
return null;
}
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < inorder.length; iPP) {
map.put(inorder[i], i);
}
return preIn(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, map);
}
private TreeNode preIn(int[] preorder, int pl, int pr, int[] inorder, int il, int ir, HashMap<Integer, Integer> map) {
if (pl > pr) {
return null;
}
TreeNode root = new TreeNode(preorder[pl]);
Integer topIndex = map.get(preorder[pl]);
root.left = preIn(preorder, pl + 1, pl + topIndex - il, inorder, il, topIndex - 1, map);
root.right = preIn(preorder, pl + topIndex + 1 - il, pr, inorder, topIndex + 1, ir, map);
return root;
}
Why does the preorder range in recursion have a-il (pl+topIndex-il) left subtree that should not be pl+1,pl+topIndex, right subtree pl+topIndex,pr?