二叉樹建樹 遍歷

2021-10-08 14:46:23 字數 1674 閱讀 6736

首先需要乙個結構體,表示每乙個結點

typedef

struct node btnode;

然後就可以直接建樹了

**示例是按前序遍歷(關於前序遍歷後面有解釋)建樹

void

build

(btnode*

&t) t-

>data = data;

t->lchild =

null

; t-

>rchild =

null

;build

(t->lchild)

;build

(t->rchild)

;}

二叉樹有四種遍歷方式

void

preorder

(btnode*

&t)//前序遍歷

}

void

inorder

(btnode*

&t)//中序遍歷

}

void

postorder

(btnode*

&t)//後序遍歷

}

void

levelorder

(btnode*

&t)//層序遍歷

}

輸入乙個如圖所示的二叉樹

輸出如圖

整體**展示

#include

#include

#include

#include

#include

#include

using

namespace std;

const

int n =

1000

;typedef

struct node btnode;

void

build

(btnode*

&t) t-

>data = data;

t->lchild =

null

; t-

>rchild =

null

;build

(t->lchild)

;build

(t->rchild);}

void

preorder

(btnode*

&t)}

void

inorder

(btnode*

&t)}

void

postorder

(btnode*

&t)}

void

levelorder

(btnode*

&t)}

intmain()

二叉樹建樹

給出前序和中序建樹 node build int n,int pre,int in 給出中序和後序建樹 node build int n,int in,int pos uva 548 給你一棵樹的中序和後序遍歷,求從根到葉子組成的路徑中數字和最小的那條,輸出最小路徑的葉子。思路 在重建完二叉樹後,d...

二叉樹建樹的過程

include using namespace std 二叉樹建樹的過程 struct tree tree creat tree root else return root tree build int ps,int is,int n tree t new tree t data preorder ...

構建二叉樹 遍歷二叉樹

陣列法構建二叉樹 public class main public static void main string args 用陣列的方式構建二叉樹 public static void createbintree 把linkedlist集合轉成二叉樹的形式 for int j 0 j 最後乙個父節...