-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
33 lines (30 loc) · 1005 Bytes
/
Solution.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
package code.JZ7;
import java.util.*;
/**
* Definition for binary tree
*/
/**给定节点数为 n 的二叉树的前序遍历和中序遍历结果,请重建出该二叉树并返回它的头结点。*/
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public TreeNode reConstructBinaryTree(int[] pre, int[] vin) {
if(pre.length == 0 || vin.length == 0) {
return null;
}
TreeNode treeNode = new TreeNode(pre[0]);
for(int i=0 ; i<vin.length ; i++) {
if(pre[0] == vin[i]) {
treeNode.left = reConstructBinaryTree
(Arrays.copyOfRange(pre, 1, i+1), Arrays.copyOfRange(vin, 0, i));
treeNode.right = reConstructBinaryTree
(Arrays.copyOfRange(pre, i+1, pre.length), Arrays.copyOfRange(vin, i + 1, vin.length));
break;
}
}
return treeNode;
}
}