二叉樹的序遍歷

2021-06-22 19:56:37 字數 802 閱讀 2320

題目描述 description

求一棵二叉樹的前序遍歷,中序遍歷和後序遍歷

輸入描述 input description

第一行乙個整數n,表示這棵樹的節點個數。

接下來n行每行2個整數l和r。第i行的兩個整數li和ri代表編號為i的節點的左兒子編號和右兒子編號。

輸出描述 output description

輸出一共三行,分別為前序遍歷,中序遍歷和後序遍歷。編號之間用空格隔開。

樣例輸入 sample input

52 3

4 50 0

0 00 0

樣例輸出 sample output

1 2 4 5 3

4 2 5 1 3

4 5 2 3 1

資料範圍及提示 data size & hint

n <= 16

#include #define right 2

#define light 1

using namespace std;

int btree[20][3];

void preot(int i) //前序遍歷

void inot(int i) //中序遍歷

void postot(int i) //後續遍歷

int main()

preot(1);

cout << endl;

inot(1);

cout << endl;

postot(1);

return 0;

}

二叉樹層序遍歷 求二叉樹的層序遍歷

給定乙個二叉樹,返回該二叉樹層序遍歷的結果,從左到右,一層一層地遍歷 例如 給定的二叉樹是,該二叉樹層序遍歷的結果是 3 9,20 15,7 示例1 輸入 返回值 1 2 示例2輸入 返回值 1 2,3 4,5 解題思路 重點是如何把在一層的節點放到一起,設定乙個引數專門放一層的節點 class t...

二叉樹遍歷之中序遍歷

1.遞迴思想 思路 1.對根結點的左子樹進行中序遍歷 2.對根結點進行訪問 3.對根結點的右子樹進行中序遍歷。參考 遞迴遍歷二叉樹 const treenode right right right var midorderrecur function root else midorder root ...

二叉樹先序遍歷和中序遍歷確定二叉樹

由於希望得到一顆二叉樹,所以返回值型別是乙個指向根節點的指標 表示得到了一顆二叉樹 btnode creatbt char pre,char int,int l1,int r1,int l2,int r2 引數列表有傳入的先序序列和後序序列和他們的開頭和結尾 由於是遞迴函式,先寫乙個遞迴出口,顯然是...