二叉樹遍歷搜尋(典例集)

2021-10-07 04:08:30 字數 4106 閱讀 3736

典例2:後序、中序遍歷還原二叉樹並進行廣度優先遍歷

典例3:後序、中序遍歷還原二叉樹並對其映象進行廣度優先遍歷

一棵二叉搜尋樹可被遞迴地定義為具有下列性質的二叉樹:對於任一結點,

其左子樹中所有結點的鍵值小於該結點的鍵值; 其右子樹中所有結點的鍵值大於等於該結點的鍵值; 其左右子樹都是二叉搜尋樹。

所謂二叉搜尋樹的「映象」,即將所有結點的左右子樹對換位置後所得到的樹。

給定乙個整數鍵值序列,現請你編寫程式,判斷這是否是對一棵二叉搜尋樹或其映象進行前序遍歷的結果。

輸入的第一行給出正整數 n(≤1000)。隨後一行給出 n 個整數鍵值,其間以空格分隔。

如果輸入序列是對一棵二叉搜尋樹或其映象進行前序遍歷的結果,則首先在一行中輸出 yes ,然後在下一行輸出該樹後序遍歷的結果。數字間有 1

個空格,一行的首尾不得有多餘空格。若答案是否,則輸出 no。

786

57108

11

yes57

681110

8

7810

11867

5

yes118

10756

8

786

85109

11

no
#include

#include

using

namespace std;

int n;

int a[

1010];

vector<

int> p;

void

dfsl

(int l,

int r)

int tr, tl;

tr = r; tl = l +1;

while

(tr > l && a[l]

<= a[tr]

)//注意邊界

tr--

;while

(tl <= r && a[l]

> a[tl]

)//注意邊界

tl++;if

(tl - tr !=1)

return

;dfsl

(l +

1, tr)

;dfsl

(tl, r)

; p.

push_back

(a[l]);

}void

dfsr

(int l,

int r)

int tr, tl;

tr = r; tl = l +1;

while

(tr > l && a[l]

> a[tr]

)//注意邊界

tr--

;while

(tl <= r && a[l]

<= a[tl]

)//注意邊界

tl++;if

(tl - tr !=1)

return

;dfsr

(l +

1, tr)

;dfsr

(tl, r)

; p.

push_back

(a[l]);

}int

main()

if(a[1]

> a[2]

)dfsl(1

, n)

;else

dfsr(1

, n);if

((int)p.

size()

!= n)

cout <<

"yes"

<< endl;

for(i =

0; i <

(int

)p.size()

;++i)

return0;

}/*78 6 5 7 10 8 11

*//*

78 10 11 8 6 7 5

*/

給定一棵二叉樹的後序遍歷和中序遍歷,請你輸出其層序遍歷的序列。這裡假設鍵值都是互不相等的正整數。

輸入第一行給出乙個正整數n(≤30),是二叉樹中結點的個數。第二行給出其後序遍歷序列。第三行給出其中序遍歷序列。數字間以空格分隔。

在一行中輸出該樹的層序遍歷的序列。數字間以1個空格分隔,行首尾不得有多餘空格。

723

1576

4123

4567

416

3572

#include

#include

#include

#include

using

namespace std;

struct bintrenode;

typedef bintrenode* pnode;

struct bintrenode};

int in[50]

;int post[50]

;queue q;

pnode createbintree

(int in,

int post,

int len)

void

bfs(pnode t)

else

if(tmp-

>left)q.

push

(tmp-

>left);if

(tmp-

>right)q.

push

(tmp-

>right);}

}int

main()

給定一棵二叉樹的中序遍歷和前序遍歷,請你先將樹做個鏡面反轉,再輸出反轉後的層序遍歷的序列。所謂鏡面反轉,是指將所有非葉結點的左右孩子對換。這裡假設鍵值都是互不相等的正整數。

輸入第一行給出乙個正整數n(≤30),是二叉樹中結點的個數。第二行給出其中序遍歷序列。第三行給出其前序遍歷序列。數字間以空格分隔。

在一行中輸出該樹反轉後的層序遍歷的序列。數字間以1個空格分隔,行首尾不得有多餘空格。

712

3456

7413

2657

461

7532

#include

#include

#include

#include

using

namespace std;

const

int maxn=55;

int post[maxn]

;int bef[maxn]

;struct bintreenode;

typedef

struct bintreenode *pnode;

queue q;

struct bintreenode

;pnode createtree

(int post,

int bef,

int len)

void

bfs(pnode t)

else

printf

(" %d"

,tmp-

>info);if

(tmp-

>rig)

q.push

(tmp-

>rig);if

(tmp-

>lef)

q.push

(tmp-

>lef);}

}int

main()

二叉樹搜尋遍歷

include include define max size 128 typedef struct bnode btree,bnode api介面 bool insertbtree btree root,bnode node 插入樹 bool deletebtree btree root,int ...

構建二叉樹 遍歷二叉樹

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

二叉樹 還原二叉樹 二叉搜尋樹

先序遍歷的特點 先遍歷根結點,再遍歷左子樹,最後再遍歷右子樹 中序遍歷的特點 先遍歷左子樹,再遍歷根結點,最後再遍歷右子樹 後序遍歷的特點 先遍歷左子樹,再遍歷右子樹,最後再遍歷根結點 舉例 先序遍歷 a b d f g h i e c 中序遍歷 f d h g i b e a c 如上,根據先序遍...