-
Notifications
You must be signed in to change notification settings - Fork 977
/
Copy pathmain.cc
38 lines (35 loc) · 886 Bytes
/
main.cc
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
#include "solution.h"
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#include <algorithm>
void print_bfs(TreeNode* p)
{
if (!p) return;
std::vector<std::string> retv;
std::queue<TreeNode *> q;
q.push(p);
do {
TreeNode *node = q.front(); q.pop();
if (node)
retv.push_back(std::to_string(node->val));
else {
retv.push_back("#");
continue;
}
q.push(node->left);
q.push(node->right);
} while (!q.empty());
auto found = std::find_if(retv.rbegin(), retv.rend(), [](const std::string &s){ return s != "#"; });
retv.erase(found.base(), retv.end());
for (const auto &s : retv)
std::cout << s << ",";
std::cout << "\b " << std::endl;
}
int main()
{
Solution s;
for (auto p : s.generateTrees(3))
print_bfs(p);
}