求二叉樹的層次遍歷(前序中序建樹,層序輸出)

2021-08-23 12:32:26 字數 747 閱讀 1531

time limit: 1000 ms memory limit: 65536 kib

submit

statistic

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

輸入資料有多組,輸入t,代表有t組測試資料。每組資料有兩個長度小於50的字串,第乙個字串為前序遍歷,第二個為中序遍歷。

每組輸出這顆二叉樹的層次遍歷。

2

abcbac

abdec

dbeac

abc

abcde

#include #include #include struct tree

*root;

struct tree *creat(char a, char b, int n);

void at_out(struct tree *root);

int main()

return 0;

}struct tree *creat(char a, char b, int n) // 根據前序和中序建樹

root->l = creat(a + 1, b, i);

root->r = creat(a + i + 1, b + i + 1, n - i - 1);

return root;

};void at_out(struct tree *root)

if(q->r != null)

}}

給定二叉樹的層次遍歷和中序遍歷 求前序遍歷

給定二叉樹的前序遍歷和中序遍歷 求後續遍歷和層次遍歷 請結合一起看 樹和二叉樹基本上都有先序 中序 後序 按層遍歷等遍歷順序,給定中序和其它一種遍歷的序列就可以確定一棵二叉樹的結構。假定一棵二叉樹乙個結點用乙個字元描述,現在給出中序和按層遍歷的字串,求該樹的先序遍歷字串。輸入格式 兩行,每行是由大寫...

二叉樹的遍歷(前序,中序,後序,層次)

圖示前序遍歷 中序遍歷 後序遍歷 層次遍歷 先序遍歷 順序 根節點 左子樹 右子樹 中序遍歷 順序 左子樹 根節點 右子樹 後序遍歷 順序 左子樹 右子樹 根節點 層次遍歷 順序 按照層次遍歷 根節點 左子樹 右子樹 利用棧的思想 如果沒有右節點,我們將會繼續出棧,繼續訪問新出棧的右節點。如果有右節...

二叉樹的前序中序,中序後序建樹

tree creat1 int len,char s1,char s2 長度 s1起始點位址 s2起始點位址 a bdfghie c t data s1 0 fdhgibe a c t l creat1 i,s1 1,s2 s1 1將先序第乙個根節點空過去 t r creat1 len i 1,s1...