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

你可能感兴趣的文章
Pytest自动化测试框架 fixture 传参实战
查看>>
pytest自动化测试框架pytest.ini配置文件详细
查看>>
Pytest自动化测试框架介绍
查看>>
Pytest自动化测试框架,建议收藏。
查看>>
Pytest自动化测试框架:mark用法---测试用例分组执行
查看>>
PyTorch 1.0 中文官方教程:强化学习 (DQN) 教程
查看>>
pytest:4种方法实现 - 重复执行用例 - 展示迭代次数
查看>>
Pytest:一个卓有成效的测试工具
查看>>
python
查看>>
Python "HTTP Error 403: Forbidden"
查看>>
python %ns的作用
查看>>
Python + Appium 之 APP 自动化测试,坑点汇总!(建议收藏)
查看>>
Python + Appium 自动化操作微信入门(超详细)
查看>>
Python + Pytest 自动化框架的用例依赖实操
查看>>
python进阶(8):yield函数
查看>>
Python + requests实现接口自动化测试!
查看>>
python + requests实现的接口自动化测试(超详细~)
查看>>
Python + selenium 如何截图?
查看>>
Python + Selenium 登录QQ邮箱
查看>>
Python + selenium如何做接口自动化测试?
查看>>