打印二叉树的叶子节点
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;
}
#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;
}
相关文章推荐
- 打印二叉树两个叶子节点间的路径
- 2013-03-17---二叉树递归,非递归实现(附代码)深度,叶子节点数量,逐行打印二叉树
- 打印二叉树两个叶子节点间的路径
- 打印二叉树两个叶子节点间的路径
- 打印二叉树从根节点到叶子节点的所有路径
- 微软等数据结构+算法面试100题(21)--二叉树打印到叶子节点的所有路径
- 打印二叉树两个叶子节点间的路径
- c++实现二叉树的查找,插入,删除,深度,叶子节点数,度为1的节点数(递归方法)及运行实例结果
- 二叉树的叶子节点与度为2的节点的关系
- 打印距离叶子节点为k的节点 Print nodes that are at distance k from a leaf node
- 输出二叉树的叶子节点
- 二叉树重建查找叶子节点的最小值
- 数叶子节点(前序建立二叉树)
- 网易——求二叉树最大叶子节点到最小叶子节点的距离
- LeetCode257 BinaryTreePaths(打印根节点到叶子节点的左右路径) Java题解
- 【数据结构】二叉树的实现(如:默认成员函数、(叶子)节点数、深度、四种遍历)
- 求二叉树从叶子到根节点的值组成数字的和
- 六:二叉树中第k层节点个数与二叉树叶子节点个数
- 基本数据结构——二叉树的建立,遍历,求叶子节点,深度计算
- 求二叉树叶子节点的个数