# 剑指offer——重建二叉树

## c++实现

/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
int prelen = pre.size();
if (prelen == 0){
return NULL;
}

vector<int> pre_left, pre_right, vin_left, vin_right;

}

for(int i = 0; i < prelen; i++){
vin_left.push_back(vin[i]);
pre_left.push_back(pre[i+1]);
}
vin_right.push_back(vin[i]);
pre_right.push_back(pre[i]);
}
}

}
};

## java实现

/**
* 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) {
int preLen = pre.length;
if(preLen == 0) return null;
TreeNode ret = new TreeNode(pre[0]);
int[] preRight = new int[preLen - headIndex-1];
int[] inRight = new int[preLen - headIndex-1];
for(int i = 0; i < preLen; i++){
preLeft[i] = pre[i+1];
inLeft[i] = in[i];
}
preRight[i - headIndex - 1] = pre[i];
inRight[i - headIndex - 1] = in[i];
}
}
ret.left = reConstructBinaryTree(preLeft, inLeft);
ret.right = reConstructBinaryTree(preRight, inRight);
return ret;
}
}
09-27 58
10-28 295

03-20 1325
07-30 58
06-13 361
05-30 47
02-19 142
06-24 599
11-25 3181
10-29 151