Skip to content

Commit

Permalink
Added problem 404. Sum of Left Leaves
Browse files Browse the repository at this point in the history
  • Loading branch information
DavyVan committed Jan 21, 2021
1 parent c335889 commit b8ec172
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 0 deletions.
14 changes: 14 additions & 0 deletions 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})
45 changes: 45 additions & 0 deletions problems/404-Sum-of-Left-Leaves/kernel.cpp
@@ -0,0 +1,45 @@
#include "kernel.h"
#include <queue>
using namespace std;

int Solution::sumOfLeftLeaves(TreeNode* root)
{
int sum = 0;
queue<TreeNode*> 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;
}
16 changes: 16 additions & 0 deletions 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);
};
24 changes: 24 additions & 0 deletions problems/404-Sum-of-Left-Leaves/main.cpp
@@ -0,0 +1,24 @@
#include "kernel.h"
#include <iostream>
#include <vector>
using namespace std;

int main()
{
int size;
cout << "Input <size>: ";
while (cin >> size)
{
vector<int> nums(size);
cout << "Input <nums>...: ";
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 <size>: ";
}
}
32 changes: 32 additions & 0 deletions problems/404-Sum-of-Left-Leaves/test.cpp
@@ -0,0 +1,32 @@
#include "kernel.h"
#include <gtest/gtest.h>
#include <vector>
using namespace std;

TEST(_404, _1)
{
vector<int> 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<int> 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);
}

0 comments on commit b8ec172

Please sign in to comment.