博客
关于我
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/

你可能感兴趣的文章
python函数拟合不规则曲线_Python计算&绘图——曲线拟合问题(转)
查看>>
python函数定义的基本格式_零基础学python-2.19 定义函数、调用函数与默认参数
查看>>
Python+Selenium之数据驱动测试的实现
查看>>
python函数定义与使用+返回值简解
查看>>
Python+Selenium登录
查看>>
Python日期时间模块
查看>>
Python函数合集:足足68个内置函数请收好!
查看>>
Python+Selenium自动化测试项目实战
查看>>
Python+Selenium自动化测试项目实战【建议收藏】
查看>>
Python+Selenium自动化测试:Page Object模式
查看>>
python+selenium进行cnblog的自动化登录测试
查看>>
Python函数只返回第一个值而不是数据框
查看>>
Python+SQL实战:京东用户行为数据分析案例解析(上)
查看>>
Python+SQL实战:京东用户行为数据分析案例解析(下)
查看>>
PYTHON+Twisted+sqlanydb=ABORT()
查看>>
Python函数之返回值、作用域和局部变量
查看>>
Python+统计学 | 探索常用的数据分析统计分布
查看>>
python, selenium中用于切换帧的函数
查看>>
python函数
查看>>
python----线程、进程、协程的区别及多线程详解
查看>>