ConstructBinaryTreeFromInorderAndPostorderTraversal

从中序与后序遍历序列构造二叉树

题目介绍

从中序与后序遍历序列构造二叉树

根据一棵树的中序遍历与后序遍历构造二叉树。

注意:
你可以假设树中没有重复的元素。

例如,给出

1
2
中序遍历 inorder = [9,3,15,20,7]
后序遍历 postorder = [9,15,7,20,3]

返回如下的二叉树:

1
2
3
4
5
  3
/ \
9 20
/ \
15 7

题目解法

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
package algorithm;

import java.util.HashMap;
import java.util.Map;

public class ConstructBinaryTreeFromInorderAndPostorderTraversal {

private Map<Integer, Integer> map;

public TreeNode buildTree(int[] inorder, int[] postorder) {

int n = inorder.length;
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) {
map.put(inorder[i], i);
}
return dfs(inorder, postorder, 0, n - 1, 0, n - 1);
}

private TreeNode dfs(int[] inorder, int[] postorder, int inorderLeft, int inorderRight,
int postOrderLeft, int postOrderRight) {
if (inorderLeft > inorderRight) {
return null;
}
int root = postorder[postOrderRight];
TreeNode rootNode = new TreeNode(root);
int index = map.get(root);
int leftSubTreeSize = index - inorderLeft;

rootNode.left = dfs(inorder, postorder, inorderLeft, index - 1,
postOrderLeft, postOrderLeft + leftSubTreeSize - 1);
rootNode.right = dfs(inorder, postorder, inorderLeft + leftSubTreeSize + 1, inorderRight,
postOrderLeft + leftSubTreeSize, postOrderRight - 1);

return rootNode;
}

public static void main(String[] args) {

}

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;
}
}
}

打印:

1

思路:

思路上,寻找根的节点位置,利用节点值唯一的特性。然后,递归构造树结构。其实也是抄上一道题的思路。


ConstructBinaryTreeFromInorderAndPostorderTraversal
https://yangtzeshore.github.io/2021/07/15/ConstructBinaryTreeFromInorderAndPostorderTraversal/
作者
Chen Peng
发布于
2021年7月15日
许可协议