[英]Vectors pointers with vector slices for Tree generation from preorder and inorder vectors
I'm getting the following error: 我收到以下错误:
solution.cpp: In member function buildTree Line 26: Char 62: error: no matching function for call to 'Solution::buildTree(__gnu_cxx::__alloc_traits, int>::value_type*, std::vector*)' root->left = buildTree(&preorder[index],&inorderslice); solution.cpp:在成员函数buildTree第26行:Char 62:错误:没有匹配函数来调用'Solution :: buildTree(__ gnu_cxx :: __ alloc_traits,int> :: value_type *,std :: vector *)'root-> left = buildTree(&preorder [index],&inorderslice); ^
^
In this code: 在这段代码中:
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if(preorder.empty() || inorder.empty())
return NULL;
if(preorder.size() == 1) // preorder and inorder should always have the same length
{
TreeNode *node = new TreeNode(preorder[0]);
return node;
}
// current root is the first entry of preorder
TreeNode *root = new TreeNode(preorder[0]);
// find the index of this number in inorder
std::vector<int>::iterator it = std::find(inorder.begin(), inorder.end(), preorder[0]);
int index = std::distance(inorder.begin(), it);
std::vector<int> inorderslice = std::vector<int>(inorder.begin(), inorder.begin() + index);
root->left = buildTree(&preorder[index],&inorderslice); // This line is a problem
root->right = buildTree(&preorder[index+1],&inorder[index+1]); // This line is a problem
return root;
}
};
I'm trying to solve a problem to generate a tree from its preorder and inorder traverse vectors. 我正在尝试解决一个问题,从它的预订和顺序遍历向量生成一个树。
I'm trying to do it recursively but I'm having a problem matching the variable types of the function doing the recursion. 我试图以递归方式执行此操作但是我遇到了匹配执行递归的函数的变量类型的问题。
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
This header is receiving a pointer to a vector of ints. 此标头正在接收指向int向量的指针。
In my algorithm I'm trying to slice the input vectors to only use parts of it in my recursion: 在我的算法中,我试图将输入向量切片为仅在我的递归中使用它的一部分:
std::vector<int> inorderslice = std::vector<int>(inorder.begin(), inorder.begin() + index);
For the preorder vector I just want to cut the elements in front (the ones before index), so I thought I'd just pass a pointer to that element of the vector. 对于预订矢量我只想剪切前面的元素(索引前的元素),所以我想我只是传递指向该元素的向量。
For the inorder vector I want to cut the elements AFTER index. 对于inorder向量我想剪切AFTER索引后的元素。
In a pythonic way this would just be preorder[index:] inorder[:index] 以pythonic方式,这只是预订[index:] inorder [:index]
But I'm getting an error in the call to the function. 但是我在调用函数时遇到错误。
How do I do this in C++? 我如何在C ++中执行此操作?
Thank you. 谢谢。
Your functions take references; 你的功能需要参考; you're passing pointers.
你正在传递指针。
There is a confusion here about what &
means. 关于什么
&
手段,这里有一个混乱。
It is any of: 它是以下任何一个:
In short, remove &
from your calls, and review the chapter in your C++ book about using references. 简而言之,从您的调用中删除
&
,并查看C ++手册中有关使用引用的章节。
This header is receiving a pointer to a vector of ints.
此标头正在接收指向int向量的指针。
No, it isn't. 不,不是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.