#include #include using namespace std; template struct Node { T value; Node *left{nullptr}, *right{nullptr}, *parent{nullptr}; Node(T value) : value(value) {} Node(T value, Node *left, Node *right) : value(value), left(left), right(right) { left->parent = right->parent = this; } void preorder_traversal_impl(Node* current, vector*>& result) { result.push_back(current); if (current->left) { preorder_traversal_impl(current->left, result); } if (current->right) { preorder_traversal_impl(current->right, result); } } // traverse the node and its children preorder // and put all the results into `result` void preorder_traversal(vector*>& result) { preorder_traversal_impl(this, result); } }; #include "gtest/gtest.h" //#include "helpers/iohelper.h" //#include "exercise.cpp" namespace { class Evaluate : public ::testing::Test {}; TEST_F(Evaluate, ExampleTest) { Node c{'c'}; Node d{'d'}; Node e{'e'}; Node b{'b', &c, &d}; Node a{'a', &b, &e}; vector*> result; a.preorder_traversal(result); ostringstream oss; for (auto n : result) oss << n->value; ASSERT_EQ("abcde", oss.str()); } } // namespace int main(int ac, char* av[]) { //::testing::GTEST_FLAG(catch_exceptions) = false; testing::InitGoogleTest(&ac, av); return RUN_ALL_TESTS(); }