資料結構實驗之求二叉樹後序遍歷和層次遍歷

2021-08-20 06:46:13 字數 1064 閱讀 1484

資料結構實驗之求二叉樹後序遍歷和層次遍歷

time limit: 1000 ms memory limit: 65536 kib

submit statistic discuss

problem description

已知一棵二叉樹的前序遍歷和中序遍歷,求二叉樹的後序遍歷和層序遍歷。

input

輸入資料有多組,第一行是乙個整數t (t<1000),代表有t組測試資料。每組包括兩個長度小於50 的字串,第乙個字串表示二叉樹的先序遍歷序列,第二個字串表示二叉樹的中序遍歷序列。

output

每組第一行輸出二叉樹的後序遍歷序列,第二行輸出二叉樹的層次遍歷序列。

sample input

2 abdegcf

dbgeafc

xnliu

lnixu

sample output

dgebfca

abcdefg

linux

xnuli

hint

source

ma6174

#include 

#include

#include

struct tree

*link[54];

char str1[54],str2[54];

struct tree*getbuild(int len,char *str1,char *str2)

root->left=getbuild(i,str1+1,str2);//左子樹的長度,左子樹在str1中的位置,左子樹在str2中的位置

root->right=getbuild(len-i-1,str1+i+1,str2+i+1);

printf("%c",root->data);

return root;

}void ceng(struct tree*root)//層序遍歷,入隊,先讓自己的左右孩子入隊後自己再出隊

i++;}}

}int main()

return

0;}

資料結構實驗之求二叉樹後序遍歷和層次遍歷

已知一棵二叉樹的前序遍歷和中序遍歷,求二叉樹的後序遍歷。輸入資料有多組,第一行是乙個整數t t 1000 代表有t組測試資料。每組包括兩個長度小於50 的字串,第乙個字串表示二叉樹的先序遍歷序列,第二個字串表示二叉樹的中序遍歷序列。每組第一行輸出二叉樹的後序遍歷序列,第二行輸出二叉樹的層次遍歷序列 ...

資料結構實驗之求二叉樹後序遍歷和層次遍歷

time limit 1000ms memory limit 65536k 已知一棵二叉樹的前序遍歷和中序遍歷,求二叉樹的後序遍歷。輸入資料有多組,第一行是乙個整數t t 1000 代表有t組測試資料。每組包括兩個長度小於50 的字串,第乙個字串表示二叉樹的先序遍歷序列,第二個字串表示二叉樹的中序遍...

資料結構實驗之求二叉樹後序遍歷和層次遍歷

資料結構實驗之求二叉樹後序遍歷和層次遍歷 time limit 1000ms memory limit 65536k 有疑問?點這裡 題目描述 已知一棵二叉樹的前序遍歷和中序遍歷,求二叉樹的後序遍歷。輸入 輸入資料有多組,第一行是乙個整數t t 1000 代表有t組測試資料。每組包括兩個長度小於50...