sdut 3341数据结构实验之二叉树二:遍历二叉树
数据结构实验之二叉树二:遍历二叉树
Problem Description
已知二叉树的一个按先序遍历输入的字符序列,如abc,,de,g,,f,,, (其中,表示空结点)。请建立二叉树并按中序和后序的方式遍历该二叉树。
Input
连续输入多组数据,每组数据输入一个长度小于50个字符的字符串。
Output
每组输入数据对应输出2行:
第1行输出中序遍历序列;
第2行输出后序遍历序列。
Example Input
abc,,de,g,,f,,,
Example Output
cbegdfacgefdba
Hint
#include <iostream>
#include <stdio.h>
#include <stack>
#include <malloc.h>
using namespace std;
typedef struct btree
{char date;struct btree *lchild,*rchild;
}btree;
char ch[51];
int i;
void createbtree(btree *&b)//引用
{char c;c=ch[i++];if(c==',')b=NULL;else{b=(btree *)malloc(sizeof(btree));b->date=c;createbtree(b->lchild);createbtree(b->rchild);}
}
void inorder(btree *&b)
{btree *p=b;stack<btree*> Stack;while(p!=NULL||!Stack.empty()){while(p!=NULL){Stack.push(p);p=p->lchild;}if(!Stack.empty()){p=Stack.top();cout<<p->date;Stack.pop();//没有返回值不能写成p=Stack.pop();p=p->rchild;}}cout<<endl;
}
void postorder(btree *b)
{btree *p=b;stack<btree*>Stack;do{while(p!=NULL){Stack.push(p);p=p->lchild;}bool flag=true;btree *r=NULL;while(!Stack.empty()&&flag){p=Stack.top();if(p->rchild==r){cout<<p->date;Stack.pop();r=p;}else{flag=false;p=p->rchild;}}}while(!Stack.empty());cout<<endl;
}
int main()
{while(cin>>ch){i=0;btree *root;root=(btree *)malloc(sizeof(btree));createbtree(root);inorder(root);postorder(root);}return 0;
}
递归:
#include <iostream>
#include <stdio.h>
#include <stack>
#include <malloc.h>
using namespace std;
typedef struct btree
{char date;struct btree *lchild,*rchild;
}btree;
char ch[51];
int i;
void createbtree(btree *&b)
{char c;c=ch[i++];if(c==',')b=NULL;else{b=(btree *)malloc(sizeof(btree));b->date=c;createbtree(b->lchild);createbtree(b->rchild);}
}
void inorder(btree *&b)
{ if(b!=NULL){inorder(b->lchild);cout<<b->date;inorder(b->rchild);}
}
void postorder(btree *b)
{if(b!=NULL){postorder(b->lchild);postorder(b->rchild);cout<<b->date;}
}
int main()
{while(cin>>ch){i=0;btree *root;root=(btree *)malloc(sizeof(btree));createbtree(root);inorder(root);cout<<endl;postorder(root);cout<<endl;}return 0;
}/***************************************************
User name: YT1658506207邵雪源
Result: Accepted
Take time: 0ms
Take Memory: 204KB
Submit time: 2017-11-02 15:21:31
****************************************************/
sdut 3341数据结构实验之二叉树二:遍历二叉树相关推荐
- SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历
数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Descript ...
- SDUT _2117 数据结构实验之链表二:逆序建立链表
点击打开链接 数据结构实验之链表二:逆序建立链表 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem D ...
- SDUT 3399 数据结构实验之排序二:交换排序
数据结构实验之排序二:交换排序 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 冒泡排序和快 ...
- 3374——数据结构实验之查找二:平衡二叉树
数据结构实验之查找二:平衡二叉树 ( LL RR LR RL) Problem Description 根据给定的输入序列建立一棵平衡二叉树,求出建立的平衡二叉树的树根. Input 输入一组测试数据 ...
- 3348 数据结构实验之数组二:稀疏矩阵
数据结构实验之数组二:稀疏矩阵 #include<iostream> #include<iomanip> using namespace std; int y,x; struc ...
- SUTD OJ 数据结构实验之查找二:平衡二叉树
数据结构实验之查找二:平衡二叉树 Time Limit: 400 ms Memory Limit: 65536 KiB Submit Statistic Problem Description 根据给 ...
- 深入学习二叉树(二) 线索二叉树
深入学习二叉树(二) 线索二叉树 1 前言 在上一篇简单二叉树的学习中,初步介绍了二叉树的一些基础知识,本篇文章将重点介绍二叉树的一种变形--线索二叉树. 2 线索二叉树 2.1 产生背景 现有一棵结 ...
- sdut 2137 数据结构实验之求二叉树后序遍历和层次遍历
数据结构实验之求二叉树后序遍历和层次遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descr ...
- sdut 2136 数据结构实验之二叉树的建立与遍历
Problem Description 已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(其中逗号表示空节点).请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树 ...
最新文章
- 用云存储30分钟快速搭建APP
- python 控制qq_最必要的最小建议集:写给刚入门编程(python)的同学
- 系统运维岗位职责和要求
- pycharm使用_后端开发使用pycharm的技巧
- Google发布文档数据库Firestore
- url 函数 php,php中url处理函数总结
- 如何解决华为手机“杀后台”严重的情况呢?
- extjs 右下角弹出消息框
- 关于事务开启与否对数据库插入数据所需时间的影响的讨论
- 5G通信演进和常见名词释义
- linux 追加多行文件,linux每行追加内容
- 微信公众平台的设计与开发之道
- AutoCAD料表提取到Excel方法介绍
- 语音助手——助手中用到的那些分类模型
- 前端学习资料网址汇总
- 学人工智能有前途吗?人工智能前景-AI就业方向
- Mac 强制退出程序方法
- 最靠谱的6个自媒体平台,也可以快速上手
- OpenSea: NFT市场的革命者
- 使用Rundll32.exe和Rundll.exe
热门文章
- [转] Deep Learning(深度学习)学习笔记整理系列
- .Net 中 获取当前应用程序启动目录的几个方法和Path.Combine 细节
- 使用RegularExpressionValidator限制多行文本框的字数
- JNI设置C++与java的结合(2)
- K8S发布解释型语言应用的最佳实践
- 访谈Stuart Davidson:Skyscanner的持续交付推广
- firewalld、netfilter、 netfilter5表5链、iptables介绍
- JavaScript提高:005:ASP.NET使用easyUI TABS标签显示问题
- 第25条:总是为第三方类的分类名称加前缀
- Mikrotik RouterOS 日常问题解析 ROS 5.4下载