您的位置:首页 > 其它

打印二叉树的叶子节点

2017-11-02 16:41 357 查看
采用先序法建立一棵二叉树,设计按先序输出二叉树的叶子,二叉树的数据域类型为字符型,扩展二叉树的叶子结点用‘#’表示,要求可以输出多棵二叉树的叶子结点,当二叉树为空时程序结束。

#include<iostream>

#include<queue>

using namespace std;

struct Node

{
char date;
Node *rightchild;
Node *leftchild;

};

Node *creat()

{
char t;

cin>>t;
if(t=='#')
{
return NULL;
}
else
{
Node *root=new Node;
root->date=t;
root->leftchild=creat();
root->rightchild=creat();
return root;
}

};

//先序 

void front(Node *root)

{
if(root==NULL)
{
return;

else
{
if(root->leftchild==NULL&&root->rightchild==NULL)
cout<<root->date<<" ";
front(root->leftchild);
front(root->rightchild);
}

}

int main()

{
Node *root[10];
int i=0;
for(;i<10;i++)
{
   root[i]=creat();
   
   if(root[i]==NULL)
   {
   break;
   }

    }

    for(int j=0;j<i;j++)

    {

    front(root[j]);

    cout<<endl;
}
cout<<"NULL";
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: