105. 从前序与中序遍历序列构造二叉树
题目
描述:
给定两个整数数组 preorder 和 inorder,其中 preorder 是二叉树的 先序遍历,inorder 是同一棵树的 中序遍历,请构造二叉树并返回其根节点。
示例 1:
- 输入:
preorder = [3,9,20,15,7],inorder = [9,3,15,20,7] - 输出:
[3,9,20,null,null,15,7]
示例 2:
- 输入:
preorder = [-1],inorder = [-1] - 输出:
[-1]
提示:
1 <= preorder.length <= 3000inorder.length == preorder.length-3000 <= preorder[i], inorder[i] <= 3000preorder和inorder均 无重复元素inorder均出现在preorderpreorder保证 为二叉树的前序遍历序列inorder保证 为二叉树的中序遍历序列
题解
用前序找根,用中序划分左右子树
前序遍历的顺序是“根、左子树、右子树”,中序遍历的顺序是“左子树、根、右子树”。因此,前序区间的第一个值确定当前根节点;在中序区间找到这个值后,左边就是左子树,右边就是右子树。
题目保证节点值互不相同,可以预先建立 indexMap,记录每个值在中序数组中的下标。这样每次定位根时无需重新扫描数组。
例如前序为 [3,9,20,15,7],中序为 [9,3,15,20,7]。前序首项 3 是根,它在中序中把数组分成 [9] 和 [15,20,7]。左子树只有一个节点,所以前序中紧随 3 的一个值 [9] 属于左子树,其余 [20,15,7] 属于右子树。
根据节点数划分下标区间
代码传递的都是两端包含的闭区间。设当前前序区间为 [preLeft, preRight],中序区间为 [inLeft, inRight],根的中序下标为 inRootIndex,左子树节点数为 leftSubtreeSize = inRootIndex - inLeft。
| 子树 | 前序区间 | 中序区间 |
|---|---|---|
| 左子树 | [preLeft + 1, preLeft + leftSubtreeSize] | [inLeft, inRootIndex - 1] |
| 右子树 | [preLeft + leftSubtreeSize + 1, preRight] | [inRootIndex + 1, inRight] |
先创建根节点,再对这两组区间分别递归,把结果连接到 root.left 和 root.right。当 preLeft > preRight 时,区间中没有节点,返回 null。例如左子树大小为 0 时,其前序区间变成 [preLeft + 1, preLeft],正好由这个条件终止。
整个过程只传下标,不复制数组;每次调用 buildTree 都重新建立索引表。建表和构造各需 $O(n)$ 时间,合计为 $O(n)$;不计输出树,索引表占 $O(n)$,递归栈占 $O(h)$,辅助空间合计为 $O(n)$。
完整示例
打印函数按层序保留中间的空位,以便核对左右子树的位置,只移除末尾无须展示的空位。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
private static Map<Integer, Integer> indexMap;
public static TreeNode buildTree(int[] preorder, int[] inorder) {
int n = preorder.length;
indexMap = new HashMap<>();
for (int i = 0; i < n; i++) {
indexMap.put(inorder[i], i);
}
return myBuildTree(preorder, 0, n - 1, inorder, 0, n - 1);
}
private static TreeNode myBuildTree(int[] preorder, int preLeft, int preRight,
int[] inorder, int inLeft, int inRight) {
if (preLeft > preRight) {
return null;
}
int rootVal = preorder[preLeft];
TreeNode root = new TreeNode(rootVal);
int inRootIndex = indexMap.get(rootVal);
int leftSubtreeSize = inRootIndex - inLeft;
root.left = myBuildTree(preorder, preLeft + 1, preLeft + leftSubtreeSize,
inorder, inLeft, inRootIndex - 1);
root.right = myBuildTree(preorder, preLeft + leftSubtreeSize + 1, preRight,
inorder, inRootIndex + 1, inRight);
return root;
}
public static void printTree(TreeNode root) {
if (root == null) {
System.out.println("[]");
return;
}
List<String> values = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node == null) {
values.add("null");
} else {
values.add(String.valueOf(node.val));
queue.offer(node.left);
queue.offer(node.right);
}
}
while (values.get(values.size() - 1).equals("null")) {
values.remove(values.size() - 1);
}
System.out.println(values);
}
public static void main(String[] args) {
int[] preorder1 = {3, 9, 20, 15, 7};
int[] inorder1 = {9, 3, 15, 20, 7};
System.out.println("示例 1 输入:");
System.out.println("preorder = [3,9,20,15,7]");
System.out.println("inorder = [9,3,15,20,7]");
TreeNode root1 = buildTree(preorder1, inorder1);
System.out.print("示例 1 输出: ");
printTree(root1);
System.out.println("-----------------------------");
int[] preorder2 = {-1};
int[] inorder2 = {-1};
System.out.println("示例 2 输入:");
System.out.println("preorder = [-1]");
System.out.println("inorder = [-1]");
TreeNode root2 = buildTree(preorder2, inorder2);
System.out.print("示例 2 输出: ");
printTree(root2);
}
}
运行结果(OpenJDK 21.0.12):
1
2
3
4
5
6
7
8
9
示例 1 输入:
preorder = [3,9,20,15,7]
inorder = [9,3,15,20,7]
示例 1 输出: [3, 9, 20, null, null, 15, 7]
-----------------------------
示例 2 输入:
preorder = [-1]
inorder = [-1]
示例 2 输出: [-1]

