Skip to content

Commit

Permalink
feature:finish leetcode-so34,so36,so54
Browse files Browse the repository at this point in the history
</subject>

Branch: dev

<type>:
- [ ] Bug fix
- [ ] Bug fix (Test)
- [x] New feature
- [ ] Breaking change
- [ ] Documentation update
- [ ] This change requires a documentation update

<body>

<footer>

Signed-off-by: Certseeds <[email protected]>
  • Loading branch information
Certseeds committed Feb 20, 2022
1 parent afecc21 commit d411992
Show file tree
Hide file tree
Showing 9 changed files with 405 additions and 10 deletions.
1 change: 1 addition & 0 deletions algorithm/tree/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ LIST(APPEND leetcode_order 104 226 530 559 589)
LIST(APPEND leetcode_order 590 617 654 669 700)
LIST(APPEND leetcode_order 701 144 145 102 101)
LIST(APPEND leetcode_order 112 235 653 so_32 so_26)
LIST(APPEND leetcode_order 113 426 so_54)
LIST(TRANSFORM leetcode_order PREPEND leetcode_)

set(dependencies ${dependencies} ${leetcode_order})
Expand Down
7 changes: 2 additions & 5 deletions algorithm/tree/Traverse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,14 @@ void in(const TreeNode *root, action func) {
if (root == nullptr) {
return;
}
stack<const TreeNode *> sta;
const TreeNode *head = root;
while (head != nullptr || !sta.empty()) {
while (head != nullptr) {
for (stack<const TreeNode *> sta; head != nullptr || !sta.empty(); head = head->right) {
for (; head != nullptr; head = head->left) {
sta.push(head);
head = head->left;
}
head = sta.top();
sta.pop();
func(head);
head = head->right;
}
}

Expand Down
62 changes: 62 additions & 0 deletions algorithm/tree/leetcode_113.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
MIT License
CS203_DSAA_template
Copyright (C) 2020-2022 nanoseeds
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "leetcode_113_test.hpp"
#include <queue>

namespace leetcode_113 {
using std::queue;


vector<vector<int>> leetcode_113::pathSum(TreeNode *root, int target) {
vector<vector<int32_t>> sums;
if (root == nullptr) {
return sums;
}
for (queue<std::pair<TreeNode *, const vector<int32_t>>> now{{{root, {root->val}}}}; !now.empty();) {
const auto[head, vec] = now.front();
now.pop();
if (head->val == target && head->left == nullptr && head->right == nullptr) {
sums.push_back(vec);
continue;
}
if (head->left != nullptr) {
vector<int32_t> path{vec};
path.push_back(head->left->val);
head->left->val += head->val;
now.emplace(head->left, path);
}
if (head->right != nullptr) {
vector<int32_t> path{vec};
path.push_back(head->right->val);
head->right->val += head->val;
now.emplace(head->right, path);
}
}
return sums;
}


}
86 changes: 86 additions & 0 deletions algorithm/tree/leetcode_113_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
MIT License
CS203_DSAA_template
Copyright (C) 2020-2022 nanoseeds
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//@Tag tree
//@Tag 树
//@description 中序遍历
//@Plan 数据结构入门 Day12
//@Plan 剑指OfferII-I Day15
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_113_TEST_HPP
#define CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_113_TEST_HPP

#include <catch_main.hpp>
#include <cstdint>
#include <cstddef>
#include <tree/treenode.hpp>
#include <tree/treenode_link.hpp>
#include <vector>

namespace leetcode_113 {
using std::vector;
using TreeNode = TREE_NODE::TreeNode<int32_t>;

struct leetcode_113 {
static vector<vector<int>> pathSum(TreeNode *root, int target);
};

using Catch::Matchers::Equals;
using TreeNodeLink = TREE_NODE::TreeNodeLink<int32_t>;

TEST_CASE("test_case 1 [test_113]", "[test_113]") {
const vector<int32_t> input{5,
4, 8,
11, TreeNode::No, 13, 4,
7, 2, TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, 5, 1
};
static constexpr const auto target{22};
const vector<TreeNode *> numVecInput = TREE_NODE::numToTree<int32_t>(input);
const vector<vector<int32_t>> output{{5, 4, 11, 2},
{5, 8, 4, 5}};
const TreeNodeLink link{numVecInput[0]};
CHECK_THAT(output, Equals(leetcode_113::pathSum(numVecInput[0], target)));
}

TEST_CASE("test_case 2 [test_113]", "[test_113]") {
const vector<int32_t> input{1,
2, 3};
static constexpr const auto target{5};
const vector<vector<int32_t>> output{};
const vector<TreeNode *> numVecInput = TREE_NODE::numToTree<int32_t>(input);
const TreeNodeLink link{numVecInput[0]};
CHECK_THAT(output, Equals(leetcode_113::pathSum(numVecInput[0], target)));
}

TEST_CASE("test_case 3 [test_113]", "[test_113]") {
const vector<int32_t> input{1,
2};
static constexpr const auto target{0};
const vector<vector<int32_t>> output{};
const vector<TreeNode *> numVecInput = TREE_NODE::numToTree<int32_t>(input);
const TreeNodeLink link{numVecInput[0]};
CHECK_THAT(output, Equals(leetcode_113::pathSum(numVecInput[0], target)));
}
}
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_113_TEST_HPP
59 changes: 59 additions & 0 deletions algorithm/tree/leetcode_426.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
MIT License
CS203_DSAA_template
Copyright (C) 2020-2022 nanoseeds
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "leetcode_426_test.hpp"
#include <stack>

namespace leetcode_426 {
using std::stack;

Node *leetcode_426::treeToDoublyList(Node *root) {
if (root == nullptr) {
return nullptr;
}
Node *base{nullptr};
Node *head = root;
bool temp{false};
for (stack<Node *> sta; head != nullptr || !sta.empty(); head = head->right) {
for (; head != nullptr; head = head->left) {
sta.push(head);
}
head = sta.top();
sta.pop();
if (base != nullptr) {
base->right = head;
}
head->left = base;
if (!temp) {
root = head;
temp = true;
}
base = head;
}
root->left = base;
base->right = root;
return root;
}
}
69 changes: 69 additions & 0 deletions algorithm/tree/leetcode_426_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
MIT License
CS203_DSAA_template
Copyright (C) 2020-2022 nanoseeds
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//@Tag tree
//@Tag 树
//@Plan 数据结构入门 Day14
//@Plan 剑指OfferII-I Day15
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_426_TEST_HPP
#define CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_426_TEST_HPP

#include <catch_main.hpp>
#include <cstdint>
#include <cstddef>
#include <tree/treenode.hpp>
#include <tree/treenode_link.hpp>
#include <vector>

namespace leetcode_426 {

using TreeNode = TREE_NODE::TreeNode<int32_t>;
using Node = TreeNode;

struct leetcode_426 {
static Node *treeToDoublyList(Node *root);
};

using TreeNodeLink = TREE_NODE::TreeNodeLink<int32_t>;
using TREE_NODE::numToTree;

TEST_CASE("test_case 1 [test_426]", "[test_426]") {
const vector<int32_t> input{4, 2, 5, 1, 3};
const vector<TreeNode *> numVecInput = numToTree<int32_t>(input);
const TreeNodeLink link{numVecInput};
leetcode_426::treeToDoublyList(numVecInput.front());
CHECK(3 == numVecInput[0]->left->val);
CHECK(5 == numVecInput[0]->right->val);
CHECK(1 == numVecInput[1]->left->val);
CHECK(3 == numVecInput[1]->right->val);
CHECK(4 == numVecInput[2]->left->val);
CHECK(1 == numVecInput[2]->right->val);
CHECK(5 == numVecInput[3]->left->val);
CHECK(2 == numVecInput[3]->right->val);
CHECK(2 == numVecInput[4]->left->val);
CHECK(4 == numVecInput[4]->right->val);
}
}
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_426_TEST_HPP
50 changes: 50 additions & 0 deletions algorithm/tree/leetcode_so_54.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
MIT License
CS203_DSAA_template
Copyright (C) 2022 nanoseeds
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "leetcode_so_54_test.hpp"
#include "traverse.cpp"

namespace leetcode_so_54 {
using namespace Tree_Traverse;

int32_t leetcode_so_54::kthLargest(TreeNode *root, int32_t k) {
vector<int32_t> countVec;
const auto functionPre = [&countVec](const TreeNode *tn) -> void {
countVec.push_back(tn->val);
};
iter::in(root, functionPre);
const auto netElements = countVec.size();
vector<int32_t> element;
static size_t count{0};
const auto function = [&element, k, netElements](const TreeNode *tn) -> void {
++count;
if (count + k == netElements + 1) { element.push_back(tn->val); }
};
iter::in(root, function);
count = 0;
return element.front();
}

}
Loading

0 comments on commit d411992

Please sign in to comment.