Skip to content

Commit 709391c

Browse files
committed
JZ4 重建二叉树
1 parent 674fff3 commit 709391c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,29 @@ public class Solution {
107107
return reverse;
108108
}
109109
}
110+
```
111+
112+
#### JZ4 重建二叉树
113+
----
114+
给定某二叉树的前序遍历和中序遍历,请重建出该二叉树并返回它的头结点。
115+
116+
```Java
117+
import java.util.Arrays;
118+
119+
public class Solution {
120+
public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
121+
if(pre.length == 0) return null;
122+
TreeNode root = new TreeNode(pre[0]);
123+
int index = 0;
124+
for(int i = 0; i < vin.length; i++){
125+
if(pre[0] == vin[i]){
126+
index = i;
127+
break;
128+
}
129+
}
130+
root.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, index + 1), Arrays.copyOfRange(vin, 0, index));
131+
root.right = reConstructBinaryTree(Arrays.copyOfRange(pre, index + 1, pre.length), Arrays.copyOfRange(vin, index + 1, vin.length));
132+
return root;
133+
}
134+
}
110135
```

0 commit comments

Comments
 (0)