-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32_2_Print_BinaryTree_inLine.cpp
More file actions
162 lines (117 loc) · 3.31 KB
/
32_2_Print_BinaryTree_inLine.cpp
File metadata and controls
162 lines (117 loc) · 3.31 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// Created by mark on 2019/7/12.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:32.2.分行从上到下打印二叉树。
2. 思路:跟层次遍历差不多,加两个变量记录节点层数和需要打印的节点个数
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <assert.h>
using namespace std;
struct BinaryTreeNode
{
int val;
BinaryTreeNode* left;
BinaryTreeNode* right;
}TN,*pTN;
// 层序遍历
void LevelOrder_InLines(BinaryTreeNode* root)
{
if(root == nullptr)
return;
queue<BinaryTreeNode*> q;
q.push(root);
int nextLevel_num = 0; // 遍历过程记录下层节点的个数
int toBePrint_num = 1; // 记录当前层待打印的个数
while(!q.empty())
{
BinaryTreeNode* pNode = q.front();
q.pop();
cout << pNode->val << " ";
--toBePrint_num; // 当前需要打印的个数减1
if(pNode->left != nullptr)
{
q.push(pNode->left);
++nextLevel_num; // 下层待打印的个数加1
}
if(pNode->right != nullptr)
{
q.push(pNode->right);
++nextLevel_num;
}
if(toBePrint_num == 0) // 当前层节点打印完,赋值下层待打印个数给toBePrint;下层待打印个数清为0;
{
toBePrint_num = nextLevel_num;
nextLevel_num = 0;
cout << endl;
}
}
}
//辅助函数 ------------------------------------------------------------------------------------------------------------------------
// 构建树节点
BinaryTreeNode* CreateTreeNode(int val)
{
BinaryTreeNode* pNode = new BinaryTreeNode();
pNode->val = val;
pNode->left = nullptr;
pNode->right = nullptr;
return pNode;
}
// 连接树节点
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight)
{
if(pParent != nullptr)
{
pParent->left = pLeft;
pParent->right = pRight;
}
}
// 销毁树
void DestroyTree(BinaryTreeNode* root)
{
if(root != nullptr)
{
BinaryTreeNode* left = root->left;
BinaryTreeNode* right = root->right;
delete root;
root = nullptr;
DestroyTree(left);
DestroyTree(right);
}
}
// 先序打印树
void PrintPreOrder(BinaryTreeNode* root)
{
if(root == nullptr)
return;
cout << root->val << " ";
PrintPreOrder(root->left);
PrintPreOrder(root->right);
}
int main(){
BinaryTreeNode* p1 = CreateTreeNode(1);
BinaryTreeNode* p2 = CreateTreeNode(2);
BinaryTreeNode* p3 = CreateTreeNode(3);
BinaryTreeNode* p4 = CreateTreeNode(4);
BinaryTreeNode* p5 = CreateTreeNode(5);
BinaryTreeNode* p6 = CreateTreeNode(6);
BinaryTreeNode* p7 = CreateTreeNode(7);
ConnectTreeNodes(p1, p2, p3);
ConnectTreeNodes(p2, p4, p5);
ConnectTreeNodes(p3, p6, nullptr);
ConnectTreeNodes(p4, p7, nullptr);
cout << "先序打印二叉树为:";
PrintPreOrder(p1);
cout << endl;
cout << "分层遍历二叉树为:" << endl;
LevelOrder_InLines(p1);
cout << endl;
return 0;
}