-
Notifications
You must be signed in to change notification settings - Fork 0
/
5.cpp
61 lines (52 loc) · 1.06 KB
/
5.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string.h>
#include <queue>
using namespace std;
typedef struct BiTNode
{
char data;
struct BiTNode* lchild, * rchild;
}BiTNode, * BiTree;
BiTree CreateBiTree(int& pos, char* str)
{// 先序建立二叉树
if (str[pos] == '0')
{
pos++;
return NULL;
}
else
{
BiTree T = new BiTNode;
T->data = str[pos];
pos++;
T->lchild = CreateBiTree(pos, str);
T->rchild = CreateBiTree(pos, str);
return T;
}
}
int Width(BiTree T)
{// 求二叉树T最大宽度
if (!T)
{
return 0;
}
int maxWidth = 0;
queue<BiTNode*> q;
q.push(T);
while (!q.empty()) {
int levelSize = q.size(); // 当前层的节点数
maxWidth = max(maxWidth, levelSize);
// 将当前层的节点依次出队,并将下一层节点入队
for (int i = 0; i < levelSize; ++i) {
BiTNode* node = q.front();
q.pop();
if (node->lchild) {
q.push(node->lchild);
}
if (node->rchild) {
q.push(node->rchild);
}
}
}
return maxWidth;
}