/<span style="font-size:18px;">**
* 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 *buildTree(vector<int> &preorder, vector<int> &inorder) {
int s = preorder.size();
if(s == 0)
return NULL;
return build(preorder,inorder,0,s-1,0,s-1);
}
int find(vector<int> &inorder, int value)//注意,最大的错误就是STL的find函数的返回值弄错,find函数返回的是指针,指针的内容是数组值,都不是索引。。。
{
for (int i = 0; i < inorder.size(); i++) {
if (value == inorder[i]) {
return i;
}
}
return -1;
}
TreeNode *build(vector<int> &preorder, vector<int> &inorder,int pre_start,int pre_end,int in_start,int in_end){
if(pre_start > pre_end)
return NULL;
//ector<int> ::iterator index = find(inorder.begin(),inorder.end(),preorder[pre_start]);
int index = find(inorder, preorder[pre_start]);
TreeNode *root = new TreeNode(0);
root->val = preorder[pre_start];
if (-1 == index)
return NULL;
int left_size = index - in_start;
root->left = build(preorder, inorder, pre_start + 1, left_size + pre_start, in_start, index - 1);
root->right = build(preorder, inorder, pre_start + left_size + 1, pre_end, index + 1, in_end);
return root;
}
};</span>
后序+中序
/<span style="font-size:18px;">**
* 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 *buildTree(vector<int> &preorder, vector<int> &inorder) {
int s = preorder.size();
if(s == 0)
return NULL;
return build(preorder,inorder,0,s-1,0,s-1);
}
int find(vector<int> &inorder, int value)//注意,最大的错误就是STL的find函数的返回值弄错,find函数返回的是指针,指针的内容是数组值,都不是索引。。。
{
for (int i = 0; i < inorder.size(); i++) {
if (value == inorder[i]) {
return i;
}
}
return -1;
}
TreeNode *build(vector<int> &preorder, vector<int> &inorder,int pre_start,int pre_end,int in_start,int in_end){
if(pre_start > pre_end)
return NULL;
//ector<int> ::iterator index = find(inorder.begin(),inorder.end(),preorder[pre_start]);
int index = find(inorder, preorder[pre_start]);
TreeNode *root = new TreeNode(0);
root->val = preorder[pre_start];
if (-1 == index)
return NULL;
int left_size = index - in_start;
root->left = build(preorder, inorder, pre_start + 1, left_size + pre_start, in_start, index - 1);
root->right = build(preorder, inorder, pre_start + left_size + 1, pre_end, index + 1, in_end);
return root;
}
};</span>
<span style="font-size:18px;">class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
int s = inorder.size();
if(s == 0)
return NULL;
return build(inorder,postorder,0,s-1,0,s-1);
}
int find(vector<int> &a,int val){
for(int i = 0;i < a.size();i++)
if(a[i] == val)
return i;
return -1;
}
TreeNode *build(vector<int> &inorder, vector<int> &postorder,int i1,int i2,int p1,int p2 ){
if(i1 > i2 || p1 > p2)
return NULL;
TreeNode *root = new TreeNode(postorder[p2]);
int index = find(inorder,postorder[p2]);
// if(index == -1)
// return NULL;
int leftsize = index - i1;
root->left = build(inorder,postorder,i1,index-1,p1,p1+leftsize-1);
root->right = build(inorder,postorder,index+1,i2,p1+leftsize,p2-1);
return root;
}
};</span>
找到递归的索引位置是最关键的部分,利用find找到根的索引值,计算左右子树长度,就可以解决问题。
<span style="font-size:18px;">class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
int s = inorder.size();
if(s == 0)
return NULL;
return build(inorder,postorder,0,s-1,0,s-1);
}
int find(vector<int> &a,int val){
for(int i = 0;i < a.size();i++)
if(a[i] == val)
return i;
return -1;
}
TreeNode *build(vector<int> &inorder, vector<int> &postorder,int i1,int i2,int p1,int p2 ){
if(i1 > i2 || p1 > p2)
return NULL;
TreeNode *root = new TreeNode(postorder[p2]);
int index = find(inorder,postorder[p2]);
// if(index == -1)
// return NULL;
int leftsize = index - i1;
root->left = build(inorder,postorder,i1,index-1,p1,p1+leftsize-1);
root->right = build(inorder,postorder,index+1,i2,p1+leftsize,p2-1);
return root;
}
};</span>
遇到的大错就是开始用错了STL的find函数,返回的是迭代器不是索引值,所以自己重写比较和谐。
http://www.cplusplus.com/reference/algorithm/find/
Returns an iterator to the first element in the range
[first,last)
that compares equal to
val
. If no such element is found, the function returns
last
.
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
first的值是内容,不是索引,first是迭代器,也不是索引!!!重建二叉树的时候犯了大错,应该重载find函数