国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 根河市| 滕州市| 巴楚县| 平湖市| 盐边县| 拜泉县| 正阳县| 玉屏| 石首市| 民勤县| 谷城县| 古蔺县| 腾冲县| 锦州市| 明水县| 绥中县| 白沙| 武胜县| 宜黄县| 收藏| 紫金县| 搜索| 汤阴县| 雷山县| 青冈县| 甘南县| 襄垣县| 昌图县| 黔西| 肃宁县| 南投县| 时尚| 梧州市| 太康县| 神池县| 娄底市| 伊春市| 屯留县| 宝山区| 睢宁县| 巴彦县|