-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathConstructBinaryTreeFromPreorderAndInorderTraversal.java
70 lines (70 loc) · 1.98 KB
/
ConstructBinaryTreeFromPreorderAndInorderTraversal.java
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
package io.ziheng.codinginterviews;
/**
* 二叉树节点
*/
class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
}
}
/**
* 剑指 Offer 面试题 06:重建二叉树(前序、中序)
*
* 题目描述:
* 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
* 假设前序遍历和中序遍历的结果中都不含重复的数字。
* 例如:
* - 前序遍历序列 {1,2,4,7,3,5,6,8}
* - 中序遍历序列 {4,7,2,1,5,3,8,6}
* 重建这颗二叉树并返回。
*
* 知识点:["树"]
*/
public class ConstructBinaryTreeFromPreorderAndInorderTraversal {
public static void main(String[] args) {
// ...
}
/**
* 剑指 Offer 面试题 06:重建二叉树(前序、中序)
*
* 时间复杂度:O(n^2)
* 空间复杂度:O(n)
*
* @param pre 前序遍历
* @param in 中序遍历
* @return TreeNode
*/
public TreeNode constructBinaryTree(int[] pre, int[] in) {
return constructBinaryTree0(pre, 0, pre.length - 1, in, 0, in.length - 1);
}
private TreeNode constructBinaryTree0(
int[] pre, int preStart, int preEnd,
int[] in, int inStart, int inEnd) {
if (preStart > preEnd || inStart > inEnd) {
return null;
}
TreeNode root = new TreeNode(pre[preStart]);
int inRoot = 0;
for (int i = inStart; i <= inEnd; i++) {
if (in[i] == pre[preStart]) {
inRoot = i;
}
}
int numsLeft = inRoot - inStart;
root.left = constructBinaryTree0(
pre, preStart + 1, preStart + numsLeft,
in, inStart, inRoot - 1
);
root.right = constructBinaryTree0(
pre, preStart + numsLeft + 1, preEnd,
in, inRoot + 1, inEnd
);
return root;
}
}
/* EOF */