打印二叉树的叶子节点

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

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

猜你喜欢

转载自blog.csdn.net/perception952/article/details/78426548