国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

數(shù)據(jù)結構實驗之二叉樹二:遍歷二叉樹

2019-11-10 18:20:27
字體:
來源:轉載
供稿:網(wǎng)友

PRoblem Description

已知二叉樹的一個按先序遍歷輸入的字符序列,如abc,,de,g,,f,,, (其中,表示空結點)。請建立二叉樹并按中序和后序的方式遍歷該二叉樹。 Input

連續(xù)輸入多組數(shù)據(jù),每組數(shù)據(jù)輸入一個長度小于50個字符的字符串。 Output

每組輸入數(shù)據(jù)對應輸出2行: 第1行輸出中序遍歷序列; 第2行輸出后序遍歷序列。

Example Input

abc,,de,g,,f,,,

Example Output

cbegdfacgefdba

Hint

Author xam

#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>using namespace std;typedef struct node{ char a; node *left; node *right;}Node;int top=0;struct node *creat(char *p)//建樹方法{ Node *root=NULL; if(top<strlen(p)) { if(p[top]!=',') { root=(Node *)malloc(sizeof(Node)); root->left=NULL; root->right=NULL; root->a=p[top++]; root->left=creat(p); root->right=creat(p); } else { top++; } } return root;}void zhong(Node *root)//中序遍歷,先序和后序類似{ if(root) { zhong(root->left); printf("%c", root->a); zhong(root->right); }}void hou(Node *root){ if(root) { hou(root->left); hou(root->right); printf("%c", root->a); }}int main(){ char p[60]; while(~scanf("%s", p)) { Node *root; top=0; root = creat(p); zhong(root); printf("/n"); hou(root); printf("/n"); }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 斗六市| 玉山县| 宁蒗| 阳高县| 塘沽区| 东宁县| 桑日县| 竹山县| 孝昌县| 马公市| 乌苏市| 台安县| 柳河县| 泰和县| 烟台市| 岳阳县| 闽清县| 海淀区| 曲水县| 兴宁市| 青田县| 甘德县| 五莲县| 五家渠市| 华阴市| 准格尔旗| 莒南县| 仪陇县| 绥江县| 固原市| 涿鹿县| 五原县| 永清县| 中宁县| 永城市| 新宁县| 明水县| 康乐县| 姚安县| 灵武市| 桂平市|