106. 从中序与后序遍历序列构造二叉树
题目
描述:
给定两个整数数组 inorder 和 postorder,其中 inorder 是二叉树的中序遍历,postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树。
示例 1:
- 输入:
inorder = [9,3,15,20,7],postorder = [9,15,7,20,3] - 输出:
[3,9,20,null,null,15,7]
示例 2:
- 输入:
inorder = [-1],postorder = [-1] - 输出:
[-1]
提示:
1 <= inorder.length <= 3000postorder.length == inorder.length-3000 <= inorder[i], postorder[i] <= 3000inorder和postorder都由 不同 的值组成postorder中每一个值都在inorder中inorder保证是树的中序遍历postorder保证是树的后序遍历
题解
从后序末尾确定根节点
后序遍历按照“左子树、右子树、根”的顺序记录节点,因此当前后序区间的最后一个值就是根。中序遍历按照“左子树、根、右子树”的顺序记录节点,根的位置可以把当前中序区间分成左右两部分。
题目保证节点值互不相同,先用 indexMap 保存每个值在中序数组中的下标,递归时便能直接找到根的位置。
例如中序为 [9,3,15,20,7]、后序为 [9,15,7,20,3]。后序末尾 3 为根;中序中 3 的左侧只有 [9],右侧是 [15,20,7]。因此后序前一个值 [9] 属于左子树,剩余的 [15,7,20] 属于右子树,末尾的根 3 不再参与子树递归。
两组数组的区间怎样对应
下面代码使用闭区间。当前中序范围为 [inLeft, inRight],后序范围为 [postLeft, postRight];根为 postorder[postRight],其中序下标为 inRootIndex,左子树大小为 leftSubtreeSize = inRootIndex - inLeft。
| 子树 | 中序区间 | 后序区间 |
|---|---|---|
| 左子树 | [inLeft, inRootIndex - 1] | [postLeft, postLeft + leftSubtreeSize - 1] |
| 右子树 | [inRootIndex + 1, inRight] | [postLeft + leftSubtreeSize, postRight - 1] |
当 inLeft > inRight 时,该子树为空,返回 null。否则创建根节点,再分别递归得到左右孩子。即使某一侧没有节点,相应的空区间也会由终止条件处理。
这份实现明确传入每棵子树的区间,所以可以先构造左子树,再构造右子树。如果采用共享下标从后序数组末尾逐个取值,下一个取到的是右子树的根,才需要改成先右后左;不能把那种写法的顺序要求直接套到这里。
建表与构造的总时间为 $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[] inorder, int[] postorder) {
int n = inorder.length;
indexMap = new HashMap<>();
for (int i = 0; i < n; i++) {
indexMap.put(inorder[i], i);
}
return myBuildTree(inorder, 0, n - 1, postorder, 0, n - 1);
}
private static TreeNode myBuildTree(int[] inorder, int inLeft, int inRight,
int[] postorder, int postLeft, int postRight) {
if (inLeft > inRight) {
return null;
}
int rootVal = postorder[postRight];
TreeNode root = new TreeNode(rootVal);
int inRootIndex = indexMap.get(rootVal);
int leftSubtreeSize = inRootIndex - inLeft;
root.left = myBuildTree(inorder, inLeft, inRootIndex - 1,
postorder, postLeft, postLeft + leftSubtreeSize - 1);
root.right = myBuildTree(inorder, inRootIndex + 1, inRight,
postorder, postLeft + leftSubtreeSize, postRight - 1);
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[] inorder1 = {9, 3, 15, 20, 7};
int[] postorder1 = {9, 15, 7, 20, 3};
System.out.println("示例 1 输入:");
System.out.println("inorder = [9,3,15,20,7]");
System.out.println("postorder = [9,15,7,20,3]");
TreeNode root1 = buildTree(inorder1, postorder1);
System.out.print("示例 1 输出: ");
printTree(root1);
System.out.println("-----------------------------");
int[] inorder2 = {-1};
int[] postorder2 = {-1};
System.out.println("示例 2 输入:");
System.out.println("inorder = [-1]");
System.out.println("postorder = [-1]");
TreeNode root2 = buildTree(inorder2, postorder2);
System.out.print("示例 2 输出: ");
printTree(root2);
}
}
运行结果(OpenJDK 21.0.12):
1
2
3
4
5
6
7
8
9
示例 1 输入:
inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]
示例 1 输出: [3, 9, 20, null, null, 15, 7]
-----------------------------
示例 2 输入:
inorder = [-1]
postorder = [-1]
示例 2 输出: [-1]

