博客
关于我
G - 数据结构实验之二叉树七:叶子问题
阅读量:770 次
发布时间:2019-03-23

本文共 938 字,大约阅读时间需要 3 分钟。

Description

已知一个按先序输入的字符序列,如abd,eg,cf,(其中,表示空结点)。请建立该二叉树并按从上到下从左到右的顺序输出该二叉树的所有叶子结点。

Input

输入数据有多行,每一行是一个长度小于50个字符的字符串。

Output

按从上到下从左到右的顺序输出二叉树的叶子结点。

#include    #include      using namespace std;typedef struct node{       char data;        node *l,*r;}Tree;char pre[55];int cnt=0;Tree* buildtree_pre(){       if(pre[cnt]==',')    {           cnt++;        return NULL;    }    Tree *root = new Tree;    root->data = pre[cnt++];    root->l = buildtree_pre();    root->r = buildtree_pre();    return root;}//思路和层次遍历差不多void leave_out(Tree *root){       queue,t;    t.push(root);    while(!t.empty())    {        root = t.front();        t.pop();        if(root)        {            if(!root->l && !root->r)                cout
l); t.push(root->r); } }}int main(){ ios::sync_with_stdio(false); Tree *root; while(cin>>pre) { cnt=0; root = buildtree_pre(); leave_out(root); }}

转载地址:http://jaezk.baihongyu.com/

你可能感兴趣的文章
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>
mysql 常用命令
查看>>
Mysql 常见ALTER TABLE操作
查看>>
MySQL 常见的 9 种优化方法
查看>>
MySQL 常见的开放性问题
查看>>
Mysql 常见错误
查看>>
mysql 常见问题
查看>>