重建二叉树 leetcode

/<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找到根的索引值,计算左右子树长度,就可以解决问题。

遇到的大错就是开始用错了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函数


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值