-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added problem 404. Sum of Left Leaves
- Loading branch information
Showing
5 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>: "; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |