【问题标题】:Given a binary tree, design an algorithm which creates a linked list of all the nodes at each depth给定一棵二叉树,设计一个算法,在每个深度创建一个所有节点的链表
【发布时间】:2015-05-01 00:27:42
【问题描述】:

我已经尝试在 C++ 中实现相同的功能。我想我几乎明白了。但是从链表访问节点时出现错误。

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++;
   }

}

返回结果; }

【问题讨论】:

  • 即使这样也不行:if(*itr->left != NULL)
  • currentparent 列表中的项目是 Node*,而不是 Node。除了您在此处显示的内容之外,对您的大部分代码一无所知,我相信您想要的语法是 (*itr)-&gt;left(*itr)-&gt;right,我高度建议您在评估 *itr 是否为 NULL >其中一个
  • 谢谢!!它现在正在工作。我也会记住你的建议。 :)
  • 很高兴它有帮助。顺便说一句,每当您处理广度算法时,队列有时可能是一种直观的工具。 See example。祝你好运。

标签: c++ linked-list binary-tree levels


【解决方案1】:

这里是上述问题的解决方案。我正在放完整的代码。

#include <iostream>
#include <list>

using namespace std;
struct Node{

Node *left;
Node *right;
int data;
Node(int d)
{
  left = NULL;
  right = NULL;
  data =d;
} 
};
class Binary_Tree{

Node *root;
list<list<Node*> > level(Node*);
public:
Binary_Tree()
{
   root = NULL;
} 

  void insert(int element);
  list<list<Node*> > level();

};
void Binary_Tree::insert(int ele)
{
 Node *node = new Node(ele); 
  if (root == NULL)
  {
    root = node;
    return;
  }
  Node* temp = root;
  while(temp != NULL)
  {
    if (ele > temp->data)
    {
      if(temp->right != NULL)
         temp = temp->right;
      else
      { 
        temp->right = node;
        return;
      }
    } 
   else
   {
      if(temp->left != NULL)
        temp = temp->left;
      else
       {
         temp->left = node;
         return;
       }
   }
  }  
}
list<list<Node*> > Binary_Tree::level()
{
   return level(root);
}
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)
      current.push_back((*itr)->left);

     if((*itr)->right != NULL)
       current.push_back((*itr)->right);

        itr++;
   }

}
return result;
}

int main()
{
list<list<Node*> >l;
list<list<Node*> >::iterator itr;
list<Node*>::iterator itr1;

Binary_Tree bt;
bt.insert(3);
bt.insert(2);
bt.insert(4);
bt.insert(1);
bt.insert(5);
l=bt.level();

itr = l.begin();
while(itr != l.end())
{

 itr1 = (*itr).begin();
 while(itr1 != (*itr).end())
 {
     cout<<(*itr1)->data<<" ";
     itr1++; 
 }
 cout<<endl;
 itr++;

} 

return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多