03-树1. List Leaves (25)

来源:互联网 发布:网络隐私权的法律保护 编辑:程序博客网 时间:2024/05/14 09:36

今日题目的,遍历树。

主要是通过队列来实现的。

下面的程序有问题的,,,,,

先挖坑在这里,求路人解答。

#include <iostream>#include <stdlib.h>#include <queue>using namespace std;#define TREE_SIZE 20/* run this program using the console pauser or add your own getch, system("pause") or input loop */typedef struct TreeNode{int data;TreeNode *left;TreeNode *right;TreeNode(){data=0;left=NULL;right=NULL;}} *BinTree;void cengxu(BinTree bt){int ans[TREE_SIZE]={0};int id=0;std::queue<BinTree> qu;qu.push(bt);while(!qu.empty()){BinTree t=qu.front();qu.pop();if(!t->left && !t->left){ans[id++]=t->data;}if(t->left){qu.push(t->left);}if(t->right){qu.push(t->right);}}for(int i=0;i<id;i++){printf("%d",ans[i]);i==id-1?printf("\n"):printf(" ");}}int main(int argc, char** argv) {int N=0;int root=0;bool flag[TREE_SIZE]={false};BinTree bt[TREE_SIZE];scanf("%d",&N);for(int i=0;i<N;i++)        bt[i]=(BinTree)malloc(sizeof(struct TreeNode));    for(int i=0;i<N;i++)        bt[i]->data=i;for(int i=0; i<N; i++){char l,r;//scanf("%c%c",&l,&r);cin>>l>>r;if(l!='-'){bt[i]->left=bt[l-'0'];flag[l-'0']=true;}else{bt[i]->left=NULL;}if(r!='-'){bt[i]->right=bt[r-'0'];flag[r-'0']=true;}else{bt[i]->right=NULL;}}for(int i=0; i<N;i++){if(flag[i]==false){root=i;break;}}cengxu(bt[root]);return 0;}




下面是一位童靴的啊

#include <iostream>#include <cstdlib>#include <set>#include <queue>#include <cstring>#include <string>using namespace std;struct node //结点结构体 {       int leftchild;  //左儿子        int rightchild;  //右儿子        node ()     //初始化        {            leftchild=-1;            rightchild=-1;       }};node n[20];bool r[20];  //记录是否是根结点 queue<int> q;   //有队列遍历树 int t=0;void traversal(int a)   //根据根节点a遍历树并且把也结点输出 {     q.push(a);     while (!q.empty())     {                      int j=q.front();           //cout <<"q.pop() "<<j<<endl;           q.pop();           if (n[j].leftchild==-1&&n[j].rightchild==-1)//没有左儿子和有儿子则为叶子            {                 if (t==0)//第一个叶节点直接输出,以后每个结点前有一个空格                  {                      cout <<j;                      t=1;                 }                 else                 {                     cout <<" "<<j;                                  }           }           if (n[j].leftchild!=-1)           {                q.push(n[j].leftchild);  //左儿子存在,压入队列            }           if (n[j].rightchild!=-1)           {                 q.push(n[j].rightchild);//左右子存在,压入队列           }     }}int main(){    char a,b;    int m;    while (cin>>m)    {          memset(r,false,sizeof(r));          for (int i=0;i<m;i++)//根据输入数据构建树           {                cin>>a>>b;                if (a>='0'&&a<='9')                {                      n[i].leftchild=a-48;                      r[a-48]=true;                }                if (b>='0'&&b<='9')                {                      n[i].rightchild=b-48;                      r[b-48]=true;                }          }          for (int i=0;i<m;i++)          {              if (!r[i])              {                  traversal(i);                  //cout <<"i"<<i<<endl;                  cout <<endl;                  break;              }                }    }    return 0;}


0 0
原创粉丝点击