Reputation: 1
Line 30: Char 11: error: no matching member function for call to 'push'
s1.push(root);
~~~^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_stack.h:233:7: note: candidate function not viable: no known conversion from 'Node *' to 'const std::stack<TreeNode *, std::deque<TreeNode *, std::allocator<TreeNode *>>>::value_type' (aka 'TreeNode *const') for 1st argument
push(const value_type& __x)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_stack.h:238:7: note: candidate function not viable: no known conversion from 'Node *' to 'std::stack<TreeNode *, std::deque<TreeNode *, std::allocator<TreeNode *>>>::value_type' (aka 'TreeNode *') for 1st argument
push(value_type&& __x)
^
public:
vector<int> postorder(Node* root) {
vector<int> v;
if(root==NULL){
return v;
}
stack<TreeNode *> s1;
stack<TreeNode *> s2;
s1.push(root);
while(!s1.empty()){
TreeNode* f=s1.top();
s1.pop();
s2.push(f);
for(int i=0;i<f->children.size();i++){
if(f->children[i]){
s1.push(f->children[i]);
}
}
}
while(!s2.empty()){
v.push_back(s2.top()->val);
s2.pop()
}
return v;
}
};```
Upvotes: 0
Views: 3617
Reputation: 415
s1
is of type stack<TreeNode *>
, which means that it can only push
values of TreeNode *
type.
But root
is of type Node*
, which is different than TreeNode *
.
Even if these types are similar ( and maybe convertible), because the function's argument is not recognized as TreeNode *
type, the function is not recognized as a whole, hence the error!
If you pay attention to the error message it is saying that the argument of the push
function should either be const value_type&
or value_type&&
. which means either const TreeNode *&
or TreeNode *&&
!
root
is of none of those types!
Upvotes: 2