# [剑指offer] 重建二叉树

#### 参考代码

```/**
* Definition for binary tree
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
if(pre.length ==0 || in.length == 0)
return null;
return ConstructBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1);
}

public TreeNode ConstructBinaryTree(int [] pre, int ps, int pe,
int [] in, int is, int ie){
if(ps > pe || is > ie){
return null;
}
TreeNode root = new TreeNode(pre);
for(int i = is; i<=ie; i++){
if(in[i] == pre){
root.left = ConstructBinaryTree(pre, ps+1, ps+i-is, in, is, i-1);
root.right = ConstructBinaryTree(pre, ps+i-is+1, pe, in, i+1, ie);
break;
}
}
return root;
}
}```

110 篇文章26 人订阅

0 条评论

## 相关文章

9520

### Leetcode 173 Binary Search Tree Iterator

mplement an iterator over a binary search tree (BST). Your iterator will be ini...

21860

### Leetcode 34 Search for a Range

Given a sorted array of integers, find the starting and ending position of a gi...

22090

14610

24350

27650

21570

### 【Java入门提高篇】Day31 Java容器类详解（十三）TreeSet详解

TreeSet是Set家族中的又一名懒将，跟其他两位一样，与对应的Map关系密不可分

10920

### 【Java入门提高篇】Day31 Java容器类详解（十三）TreeSet详解

TreeSet是Set家族中的又一名懒将，跟其他两位一样，与对应的Map关系密不可分

9830

11110