LC.P889[根据前序和后序遍历构造二叉树]

方法一:DFS+哈希表

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
/**
* Definition for a binary tree node.
* public 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;
* }
* }
*/
class Solution {
int[] preorder;
Map<Integer, Integer> map = new HashMap<>();

public TreeNode constructFromPrePost(int[] preorder, int[] postorder) {
this.preorder = preorder;
int n = preorder.length;
for (int i = 0; i < n; ++i) {
map.put(postorder[i], i);
}
return dfs(0, n - 1, 0, n - 1);
}

private TreeNode dfs(int preorderLeft, int preorderRight, int postorderLeft, int postorderRight) {
if (preorderLeft > preorderRight) return null;
TreeNode root = new TreeNode(preorder[preorderLeft]);
if (preorderLeft == preorderRight) return root; // 叶子节点
int postorderRoot = map.get(preorder[preorderLeft + 1]); // 左子树的根节点在后序遍历中的位置
int leftSubTreeSize = postorderRoot - postorderLeft + 1;
root.left = dfs(preorderLeft + 1, preorderLeft + leftSubTreeSize, postorderLeft, postorderRoot);
root.right = dfs(preorderLeft + leftSubTreeSize + 1, preorderRight, postorderRoot + 1, postorderRight - 1);
return root;
}
}
  • 时间复杂度:$O(n)$
  • 空间复杂度:$O(n)$