-
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.
Signed-off-by: Certseeds <[email protected]>
- Loading branch information
Showing
7 changed files
with
199 additions
and
129 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
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,30 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2020-2023 nanoseeds | ||
*/ | ||
#include "leetcode_unknown_test.hpp" | ||
#include "traverse.cpp" | ||
|
||
namespace leetcode_unknown { | ||
using namespace Tree_Traverse; | ||
|
||
bool leetcode_unknown::findTarget(TreeNode *root, int k) { | ||
vector<int32_t> targets{}; | ||
const auto func = [&targets](const TreeNode *tn) -> void { targets.push_back(tn->val); }; | ||
iter::in(root, func); | ||
for (size_t ll{0}, rr{targets.size() - 1}; ll < rr;) { | ||
const auto sum = targets[ll] + targets[rr]; | ||
if (sum == k) { | ||
return true; | ||
} else if (sum > k) { | ||
--rr; | ||
} else if (sum < k) { | ||
++ll; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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,41 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2020-2023 nanoseeds | ||
*/ | ||
//@Tag tree | ||
//@Tag 树 | ||
//@Plan 数据结构入门 Day14 | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_UNKNOWN_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_UNKNOWN_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <tree/treenode.hpp> | ||
#include <tree/treenode_link.hpp> | ||
#include <vector> | ||
|
||
namespace leetcode_unknown { | ||
|
||
using TreeNode = TREE_NODE::TreeNode<int32_t>; | ||
|
||
struct leetcode_unknown { | ||
static bool findTarget(TreeNode *root, int k); | ||
}; | ||
|
||
using TreeNodeLink = TREE_NODE::TreeNodeLink<int32_t>; | ||
|
||
TEST_CASE("test_case 1 [test_235]", "[test_235]") { | ||
const TreeNodeLink input{5, | ||
3, 6, | ||
2, 4, TreeNode::No, 7, | ||
TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, | ||
TreeNode::No, TreeNode::No}; | ||
static constexpr const auto target{9}; | ||
CHECK(leetcode_unknown::findTarget(input[0], target)); | ||
} | ||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_UNKNOWN_TEST_HPP |