I have tried implementing same in c++. I think I almost got it.However I am getting an error while accessing node from a linked list.
list<list<Node*> > Binary_Tree::level(Node* node)
{
list<Node*> current,parent;
list<list<Node*> > result;
list<Node*>::iterator itr;
if (node != NULL)
current.push_back(node);
while(current.size() > 0)
{
result.push_back(current);
parent = current;
current.clear();
itr = parent.begin();
while(itr != parent.end())
{
if(itr->left != NULL) // I am getting an error here.error: request for member ‘right’ in ‘* itr.std::_List_iterator<_Tp>::operator-><Node*>()’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?
current.push_back(itr->left);
if(itr->right != NULL)
current.push_back(itr->right);
itr++;
}
}
return result; }
currentandparentlists areNode*, notNode. Knowing nothing about most of your code beyond what you display here, I believe the syntax you want is(*itr)->leftand(*itr)->right, and I highly advise you check*itragainst NULL before evaluating either of those.