-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
93 lines (76 loc) · 1.52 KB
/
main.cpp
File metadata and controls
93 lines (76 loc) · 1.52 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
// Ö®×Ö´òÓ¡¶þ²æÊ÷
#include <vector>
#include <queue>
#include <string>
using std::vector;
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(nullptr), right(nullptr) {
}
};
vector<vector<int> > Print(TreeNode* pRoot)
{
vector<vector<int> > vec;
if (pRoot)
{
std::deque<TreeNode *> qu;
qu.push_back(nullptr);
qu.push_back(pRoot);
bool leftToRight = true;
while (qu.size() != 1)
{
TreeNode *node = qu.front();
qu.pop_front();
vector<int> floor;
if (node == nullptr)
{
if (leftToRight)
{
for (auto it = qu.begin(); it != qu.end(); ++it)
{
floor.push_back((*it)->val);
}
}
else
{
for (auto it = qu.rbegin(); it != qu.rend(); ++it)
{
floor.push_back((*it)->val);
}
}
leftToRight = !leftToRight;
vec.push_back(floor);
floor.clear();
qu.push_back(nullptr);
continue;
}
if (node->left)
qu.push_back(node->left);
if (node->right)
qu.push_back(node->right);
}
}
return vec;
}
int main()
{
TreeNode *root = new TreeNode(1);
TreeNode *root1 = new TreeNode(2);
TreeNode *root2 = new TreeNode(3);
TreeNode *root3 = new TreeNode(4);
TreeNode *root4 = new TreeNode(5);
TreeNode *root5 = new TreeNode(6);
TreeNode *root6 = new TreeNode(7);
//TreeNode *root7 = new TreeNode(8);
root->left = root1;
root->right = root2;
root1->left = root3;
root1->right = root4;
root2->left = root5;
root2->right = root6;
Print(root);
return 0;
}