-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathleetcode102_binary_level_order_traversal.cpp
135 lines (113 loc) · 3.25 KB
/
leetcode102_binary_level_order_traversal.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
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
/**
* @file leetcode102_binary_tree_level_order_traversal.cpp
* @author wangguibao(https://github.com/wangguibao)
* @date 2023/06/11 09:03
* @brief https://leetcode.com/problems/binary-tree-level-order-traversal
*
**/
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
/**
* Definition for a binary tree node.
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
std::vector<std::vector<int>> ret;
if (root == nullptr) {
return ret;
}
std::vector<NodeInfo> nodeVec;
std::queue<NodeInfo> q;
NodeInfo info;
info.node = root;
info.level = 0;
q.push(info);
while (!q.empty()) {
auto info = q.front();
q.pop();
nodeVec.emplace_back(info);
if (info.node->left) {
NodeInfo leftNode;
leftNode.node = info.node->left;
leftNode.level = info.level + 1;
q.push(leftNode);
}
if (info.node->right) {
NodeInfo rightNode;
rightNode.node = info.node->right;
rightNode.level = info.level + 1;
q.push(rightNode);
}
}
size_t size = nodeVec.size();
size_t i = 0;
int level = nodeVec[0].level;
std::vector<int> levelVec;
levelVec.push_back(nodeVec[0].node->val);
for (i = 1; i < size; ++i) {
if (nodeVec[i].level != level) {
ret.push_back(levelVec);
levelVec.clear();
level = nodeVec[i].level;
}
levelVec.push_back(nodeVec[i].node->val);
}
ret.push_back(levelVec);
return ret;
}
private:
struct NodeInfo {
TreeNode* node;
int level;
};
};
TreeNode* construct_tree() {
TreeNode* root = new TreeNode(5);
root->left = new TreeNode(1);
root->right = new TreeNode(4);
auto right = root->right;
right->left = new TreeNode(3);
right->right = new TreeNode(6);
return root;
}
void print_tree(TreeNode* root) {
std::queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
TreeNode* node = q.front();
q.pop();
std::cout << node->val << " ";
if (node->left) {
q.push(node->left);
}
if (node->right) {
q.push(node->right);
}
}
std::cout << std::endl;
}
int main() {
TreeNode* root = construct_tree();
Solution solution;
auto ret = solution.levelOrder(root);
for (size_t i = 0; i < ret.size(); ++i) {
std::cout << "{";
for (size_t j = 0; j < ret[i].size(); ++j) {
std::cout << ret[i][j] << " ";
}
std::cout << "}" << std::endl;
}
return 0;
}