二叉樹的建立和三種遍歷

2021-09-29 18:05:14 字數 1089 閱讀 9732

二叉樹的三種遍歷是以根的遍歷順序來講的

先序遍歷(根左右)

中序遍歷(左根右)

後序遍歷(左右根)

二叉樹是從根節點的那一層開始,從左至右讀每一層的結點讀入的。@表示空結點,#表示輸入結束

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define inf 0x3f3f3f3f

using

namespace std;

typedef

long

long ll;

typedef

unsigned

long

long ull;

const

int maxn =

1e5+5;

const

int maxm =1;

struct bitree

*tree[maxn]

;bitree *

creat_bitree()

tree[

++ rear]

= tmp;

if(rear ==

1) root = tmp;

else

if(rear %2)

//tree[front]的左右孩子處理完了

front ++;}

ch =

getchar()

;}return root;

}void

preorder

(bitree *p)

}void

midorder

(bitree *p)

}void

lastorder

(bitree *p)

}int

main()

/*abcd@ef@g@@@@h#

*/

二叉樹建立和遍歷

二叉樹建立遍歷規則 1.先序 根 左 右 2.中序 左 根 右 3.後序 左 右 根 二叉樹定義和輔助函式如下 struct node void visit int data int indata 先序建立二叉樹 struct node createbitree 先序建立乙個二叉樹 return t...

二叉樹建立和遍歷

include include 帶返回值建立二叉樹 最簡單方法 節點資料結構 struct bs node typedef struct bs node tree tree head,p,root 建立二元查詢樹 有返回值的可以不傳參 沒有的話如何傳參 輸入0代表到了某個葉子節點 tree crea...

二叉樹建立和遍歷

二叉樹建立遍歷規則 1.先序 根 左 右 2.中序 左 根 右 3.後序 左 右 根 二叉樹定義和輔助函式例如以下 struct node void visit int data int indata 先序建立二叉樹 struct node createbitree 先序建立乙個二叉樹 return...