# 劍指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``:`
`    ``struct TreeNode* reConstructBinaryTree(vector<``int``> pre,vector<``int``> in) {`
`        ``int`
`len = in.size();`
`        ``if``(len == ``0``){`
`            ``return`
`NULL;`
`        ``}`
`        ``vector<``int``> left_pre, right_pre; `
`        ``vector<``int``> left_in, right_in; `
`        ``TreeNode *head = ``new` `TreeNode(pre[``0``]);`
`        ``int`
`index = ``0``;`
`        ``for``(``int`
`i=``0``; i<len; i ){`
`            ``if``(in[i] == pre[``0``]){`
`                ``index = i;`
`                ``break``;`
`            ``}`
`        ``}`
`        `
`        ``for``(``int`
`i=``0``; i<index; i ){`
`            ``left_pre.push_back(pre[i ``1``]);`
`            ``left_in.push_back(in[i]);`
`        ``}`
`        ``for``(``int`
`i=index ``1``; i<len; i ){`
`            ``right_pre.push_back(pre[i]);`
`            ``right_in.push_back(in[i]);`
`        ``}`
`        `
`        ``//TreeNode* root = new TreeNode(pre[0]);`
`        ``head->left = reConstructBinaryTree(left_pre, left_in);`
`        ``head->right = reConstructBinaryTree(right_pre, right_in);        `
`        `
`        ``return`
`head;`
`    ``}`
`};`