为什么已经声明的变量传递给函数后提示未声明?
在做剑指offer的二叉树重构题目时遇到了如下问题:
int searchInt(vector<int> vec, int val)
int index = 0;
for (;*iter != val;++iter)//不用考虑查找失败,因为一定会查找成功
++index;
return index;
}
TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin)
{
int n = pre.size();
auto it_pre = pre.begin();
auto it_vin = vin.begin();
if (n>0)
{
TreeNode *t = new TreeNode(*it_pre);
int mid = searchInt(vin, t->val);
//借助vector的范围拷贝
{auto iter = vec.begin();
int index = 0;
for (;*iter != val;++iter)//不用考虑查找失败,因为一定会查找成功
++index;
return index;
}
TreeNode* reConstructBinaryTree(vector<int> pre, vector<int> vin)
{
int n = pre.size();
auto it_pre = pre.begin();
auto it_vin = vin.begin();
if (n>0)
{
TreeNode *t = new TreeNode(*it_pre);
int mid = searchInt(vin, t->val);
//借助vector的范围拷贝
vector<int> LnewPre;//传给左子树的新vector
vector<int> LnewIn;
vector<int> RnewPre;//传给右子树的新vector
vector<int> RnewIn;
LnewPre.assign(it_pre + 1, it_pre + 1 + mid);
LnewIn.assign(it_vin, it_vin + mid);
RnewPre.assign(it_pre + mid + 1, it_pre + n);//终点为it_pre+mid+1+n-mid-1
RnewIn.assign(it_vin + mid + 1, it_vin + n);//终点为it_vin+mid+1+n-mid-1
t->left = reConstructBinaryTree(LnewPre,LnewIn);//注意提给参数是vector
t->right = reConstructBinaryTree(RnewPre,RnewIn);
return t;
}
return nullptr;
}
vector<int> LnewIn;
vector<int> RnewPre;//传给右子树的新vector
vector<int> RnewIn;
LnewPre.assign(it_pre + 1, it_pre + 1 + mid);
LnewIn.assign(it_vin, it_vin + mid);
RnewPre.assign(it_pre + mid + 1, it_pre + n);//终点为it_pre+mid+1+n-mid-1
RnewIn.assign(it_vin + mid + 1, it_vin + n);//终点为it_vin+mid+1+n-mid-1
t->left = reConstructBinaryTree(LnewPre,LnewIn);//注意提给参数是vector
t->right = reConstructBinaryTree(RnewPre,RnewIn);
return t;
}
return nullptr;
}
这里的LnewPre,LnewIn等4个vector都是声明过了的,但是传给递归的reConstructBinaryTree(LnewPre,LnewIn)时提示LnewPre,LnewIn未声明,这是为什么呢?