diff --git a/problems/404-Sum-of-Left-Leaves/CMakeLists.txt b/problems/404-Sum-of-Left-Leaves/CMakeLists.txt new file mode 100644 index 0000000..5f85c55 --- /dev/null +++ b/problems/404-Sum-of-Left-Leaves/CMakeLists.txt @@ -0,0 +1,14 @@ +add_executable(404 main.cpp kernel.cpp) + +if(T AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp) + add_executable(404_test kernel.cpp test.cpp) + target_link_libraries(404_test gtest_main) + # add_test(NAME 4041 COMMAND 4041_test) + include(GoogleTest) + gtest_discover_tests(404_test) +else(T AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp) + message(STATUS "Test building is skipped for 404") +endif(T AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp) + + +install(TARGETS 404 DESTINATION ${INSTALL_DIR}) diff --git a/problems/404-Sum-of-Left-Leaves/kernel.cpp b/problems/404-Sum-of-Left-Leaves/kernel.cpp new file mode 100644 index 0000000..caab460 --- /dev/null +++ b/problems/404-Sum-of-Left-Leaves/kernel.cpp @@ -0,0 +1,45 @@ +#include "kernel.h" +#include +using namespace std; + +int Solution::sumOfLeftLeaves(TreeNode* root) +{ + int sum = 0; + queue q; // BFS + bool isLeft = true; + + // root + if (root == nullptr) + return 0; + if (root->left == nullptr && root->right == nullptr) + return 0; + else + { + q.push(root->left); + q.push(root->right); + } + + while (!q.empty()) + { + TreeNode *cur = q.front(); + if (cur != nullptr) + { + if (cur->left == nullptr && cur->right == nullptr) + { + if (isLeft) + sum += cur->val; + } + else + { + q.push(cur->left); + q.push(cur->right); + } + + } + + q.pop(); + isLeft = !isLeft; + } + + return sum; +} \ No newline at end of file diff --git a/problems/404-Sum-of-Left-Leaves/kernel.h b/problems/404-Sum-of-Left-Leaves/kernel.h new file mode 100644 index 0000000..08f7005 --- /dev/null +++ b/problems/404-Sum-of-Left-Leaves/kernel.h @@ -0,0 +1,16 @@ +#include "utils/binary_tree.h" +/** + * 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: + static int sumOfLeftLeaves(TreeNode* root); +}; diff --git a/problems/404-Sum-of-Left-Leaves/main.cpp b/problems/404-Sum-of-Left-Leaves/main.cpp new file mode 100644 index 0000000..966b480 --- /dev/null +++ b/problems/404-Sum-of-Left-Leaves/main.cpp @@ -0,0 +1,24 @@ +#include "kernel.h" +#include +#include +using namespace std; + +int main() +{ + int size; + cout << "Input : "; + while (cin >> size) + { + vector nums(size); + cout << "Input ...: "; + for (int i = 0; i < size; i++) + cin >> nums[i]; + + BinaryTree bt; + TreeNode* root = bt.construct_from_level_order_array(nums.data(), size); + int ret = Solution::sumOfLeftLeaves(root); + printf("%d\n", ret); + + cout << "Input : "; + } +} \ No newline at end of file diff --git a/problems/404-Sum-of-Left-Leaves/test.cpp b/problems/404-Sum-of-Left-Leaves/test.cpp new file mode 100644 index 0000000..697bf7e --- /dev/null +++ b/problems/404-Sum-of-Left-Leaves/test.cpp @@ -0,0 +1,32 @@ +#include "kernel.h" +#include +#include +using namespace std; + +TEST(_404, _1) +{ + vector nums{3, 9, 20, -1, -1, 15, 7}; + int expect = 24; + + BinaryTree bt; + TreeNode *root = bt.construct_from_level_order_array(nums.data(), nums.size()); + EXPECT_EQ(Solution::sumOfLeftLeaves(root), expect); +} + +TEST(_404, _2) +{ + vector nums{3}; + int expect = 0; + + BinaryTree bt; + TreeNode *root = bt.construct_from_level_order_array(nums.data(), nums.size()); + EXPECT_EQ(Solution::sumOfLeftLeaves(root), expect); +} + +TEST(_404, _3) +{ + int expect = 0; + + BinaryTree bt; + EXPECT_EQ(Solution::sumOfLeftLeaves(nullptr), expect); +} \ No newline at end of file