You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
969 B
36 lines
969 B
5 years ago
|
---
|
||
|
二叉树相关
|
||
|
---
|
||
|
|
||
|
#### 目录
|
||
|
|
||
|
1. #### [07. 重建二叉树](https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/)
|
||
|
|
||
|
#### [07. 重建二叉树](https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/)
|
||
|
|
||
|
```java
|
||
|
class Solution {
|
||
|
|
||
|
public TreeNode buildTree(int[] preorder, int[] inorder) {
|
||
|
int n = preorder.length;
|
||
|
if (n == 0) {
|
||
|
return null;
|
||
|
}
|
||
|
int rootVal = preorder[0], rootIndex = 0;
|
||
|
for (int i = 0; i < n; i++) {
|
||
|
if (inorder[i] == rootVal) {
|
||
|
rootIndex = i;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
TreeNode root = new TreeNode(rootVal);
|
||
|
root.left = buildTree(Arrays.copyOfRange(preorder, 1, 1 + rootIndex),
|
||
|
Arrays.copyOfRange(inorder, 0, rootIndex));
|
||
|
root.right = buildTree(Arrays.copyOfRange(preorder, 1 + rootIndex, n),
|
||
|
Arrays.copyOfRange(inorder, rootIndex + 1, n));
|
||
|
return root;
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|