forked from pezy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
36 lines (33 loc) · 875 Bytes
/
main.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
#include "solution.h"
#include <iostream>
#include <queue>
void print_bfs(TreeNode* p)
{
if (!p) return;
std::vector<std::string> v;
std::queue<TreeNode *> q;
q.push(p);
do {
TreeNode *node = q.front(); q.pop();
if (node)
v.push_back(std::to_string(node->val));
else {
v.push_back("#");
continue;
}
q.push(node->left);
q.push(node->right);
} while (!q.empty());
auto found = std::find_if(v.rbegin(), v.rend(), [](const std::string &s){ return s != "#"; });
v.erase(found.base(), v.end());
for (const auto &s : v)
std::cout << s << ",";
std::cout << "\b " << std::endl;
}
int main()
{
Solution s;
std::vector<int> preorder{1,2,4,5,3,6};
std::vector<int> inorder{4,2,5,1,3,6};
print_bfs(s.buildTree(preorder, inorder));
}