2022-09-04 23:02:44 +02:00
|
|
|
#include <deque>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
// Definition for a binary tree node.
|
|
|
|
struct TreeNode {
|
|
|
|
int val;
|
2024-01-03 12:06:42 +01:00
|
|
|
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) {}
|
2022-09-04 23:02:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct TreeNodeHandle {
|
2024-01-03 12:06:42 +01:00
|
|
|
TreeNode *node;
|
2022-09-04 23:02:44 +02:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
TreeNodeHandle(TreeNode *node, int x, int y) : node(node), x(x), y(y) {}
|
2022-09-04 23:02:44 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
bool operator==(const TreeNodeHandle &rhs) const {
|
2022-09-04 23:02:44 +02:00
|
|
|
return node == rhs.node && x == rhs.x && y == rhs.y;
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
int value() const { return node->val; }
|
2022-09-04 23:02:44 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
bool operator<(const TreeNodeHandle &rhs) const {
|
2022-09-04 23:02:44 +02:00
|
|
|
if (y != rhs.y) {
|
|
|
|
return y < rhs.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x != rhs.x) {
|
|
|
|
return x < rhs.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value() < rhs.value();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class verticals {
|
2024-01-03 12:06:42 +01:00
|
|
|
TreeNode *root;
|
2022-09-04 23:02:44 +02:00
|
|
|
|
|
|
|
class verticals_iter {
|
|
|
|
std::deque<TreeNodeHandle> queue;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
void advance() {
|
|
|
|
auto &n = queue.front();
|
2022-09-04 23:02:44 +02:00
|
|
|
|
|
|
|
if (n.node->left != nullptr) {
|
|
|
|
queue.push_back(TreeNodeHandle(n.node->left, n.x - 1, n.y + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n.node->right != nullptr) {
|
2024-01-03 12:06:42 +01:00
|
|
|
queue.push_back(
|
|
|
|
TreeNodeHandle(n.node->right, n.x + 1, n.y + 1));
|
2022-09-04 23:02:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
verticals_iter(std::deque<TreeNodeHandle> queue) : queue(queue) {
|
2022-09-04 23:02:44 +02:00
|
|
|
if (queue.front().node == nullptr) {
|
|
|
|
queue.pop_front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
bool operator!=(const verticals_iter &other) const {
|
2022-09-04 23:02:44 +02:00
|
|
|
return queue != other.queue;
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
verticals_iter operator++() {
|
2022-09-04 23:02:44 +02:00
|
|
|
advance();
|
|
|
|
queue.pop_front();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
TreeNodeHandle &operator*() { return queue.front(); }
|
2022-09-04 23:02:44 +02:00
|
|
|
};
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
verticals(TreeNode *root) : root(root) {}
|
2022-09-04 23:02:44 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
verticals_iter begin() const {
|
|
|
|
return verticals_iter(std::deque{TreeNodeHandle(root, 0, 0)});
|
2022-09-04 23:02:44 +02:00
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
verticals_iter end() const {
|
2022-09-04 23:02:44 +02:00
|
|
|
std::deque<TreeNodeHandle> q;
|
|
|
|
return verticals_iter(q);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
} // namespace
|
2022-09-04 23:02:44 +02:00
|
|
|
|
|
|
|
class Solution {
|
2024-01-03 12:06:42 +01:00
|
|
|
public:
|
|
|
|
std::vector<std::vector<int>> verticalTraversal(TreeNode *root) {
|
2022-09-04 23:02:44 +02:00
|
|
|
std::map<int, std::vector<TreeNodeHandle>> traversals;
|
|
|
|
|
|
|
|
int min_x = 0;
|
|
|
|
int max_x = 0;
|
|
|
|
|
|
|
|
for (auto node : verticals(root)) {
|
|
|
|
traversals[node.x].push_back(node);
|
|
|
|
|
|
|
|
min_x = std::min(min_x, node.x);
|
|
|
|
max_x = std::max(max_x, node.x);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::vector<int>> result;
|
|
|
|
|
|
|
|
for (int x = min_x; x <= max_x; x++) {
|
2024-01-03 12:06:42 +01:00
|
|
|
auto &v = traversals[x];
|
2022-09-04 23:02:44 +02:00
|
|
|
|
|
|
|
if (v.size()) {
|
|
|
|
std::sort(v.begin(), v.end());
|
2024-01-03 12:06:42 +01:00
|
|
|
result.push_back(std::vector<int>{});
|
2022-09-04 23:02:44 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
for (auto &n : v) {
|
2022-09-04 23:02:44 +02:00
|
|
|
result.back().push_back(n.value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma region tests
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
namespace _tests {
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
TreeNode *construct_tree(const std::vector<int> &values, std::size_t i = 0) {
|
2022-09-04 23:02:44 +02:00
|
|
|
if (i >= values.size() || values[i] == -1) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto tree = new TreeNode(values[i]);
|
|
|
|
tree->left = construct_tree(values, 2 * i + 1);
|
|
|
|
tree->right = construct_tree(values, 2 * i + 2);
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
void destruct_tree(TreeNode *ptr) {
|
2022-09-04 23:02:44 +02:00
|
|
|
if (ptr == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
destruct_tree(ptr->left);
|
|
|
|
destruct_tree(ptr->right);
|
|
|
|
delete ptr;
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
} // namespace _tests
|
2022-09-04 23:02:44 +02:00
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
TEST(examples, first) {
|
2022-09-04 23:02:44 +02:00
|
|
|
Solution s;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
auto t = _tests::construct_tree(std::vector{3, 9, 20, -1, -1, 15, 7});
|
|
|
|
EXPECT_EQ(s.verticalTraversal(t),
|
|
|
|
(std::vector{std::vector{9}, std::vector{3, 15}, std::vector{20},
|
|
|
|
std::vector{7}}));
|
2022-09-04 23:02:44 +02:00
|
|
|
_tests::destruct_tree(t);
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
TEST(examples, second) {
|
2022-09-04 23:02:44 +02:00
|
|
|
Solution s;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
auto t = _tests::construct_tree(std::vector{1, 2, 3, 4, 5, 6, 7});
|
|
|
|
EXPECT_EQ(s.verticalTraversal(t),
|
|
|
|
(std::vector{std::vector{4}, std::vector{2}, std::vector{1, 5, 6},
|
|
|
|
std::vector{3}, std::vector{7}}));
|
2022-09-04 23:02:44 +02:00
|
|
|
_tests::destruct_tree(t);
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
TEST(examples, third) {
|
2022-09-04 23:02:44 +02:00
|
|
|
Solution s;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
auto t = _tests::construct_tree(std::vector{1, 2, 3, 4, 6, 5, 7});
|
|
|
|
EXPECT_EQ(s.verticalTraversal(t),
|
|
|
|
(std::vector{std::vector{4}, std::vector{2}, std::vector{1, 5, 6},
|
|
|
|
std::vector{3}, std::vector{7}}));
|
2022-09-04 23:02:44 +02:00
|
|
|
_tests::destruct_tree(t);
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
TEST(submission, first) {
|
2022-09-04 23:02:44 +02:00
|
|
|
Solution s;
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
auto t = _tests::construct_tree(std::vector{3, 1, 4, 0, 2, 2});
|
|
|
|
EXPECT_EQ(s.verticalTraversal(t),
|
|
|
|
(std::vector{std::vector{0}, std::vector{1}, std::vector{3, 2, 2},
|
|
|
|
std::vector{4}}));
|
2022-09-04 23:02:44 +02:00
|
|
|
_tests::destruct_tree(t);
|
|
|
|
}
|
|
|
|
|
2024-01-03 12:06:42 +01:00
|
|
|
int main(int argc, char **argv) {
|
2022-09-04 23:02:44 +02:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma endregion /* tests */
|