Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve Problem 2471. Minimum Number of Operations to Sort a Binary Tree by Level #2298

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ LeetSpace serves as a dedicated workspace and archive for my [LeetCode](https://
| [2441. Largest Positive Integer That Exists With Its Negative](https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/) | Easy | [C](./old-problems/2441/c/solution.c) [C++](./old-problems/2441/solution.cpp) |
| [2444. Count Subarrays With Fixed Bounds](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/) | Hard | [C](./old-problems/2444/c/solution.c) [C++](./old-problems/2444/solution.cpp) |
| [2467. Most Profitable Path in a Tree](https://leetcode.com/problems/most-profitable-path-in-a-tree/) | Medium | [C](./old-problems/2467/c/solution.c) [C++](./old-problems/2467/solution.cpp) |
| [2471. Minimum Number of Operations to Sort a Binary Tree by Level](https://leetcode.com/problems/minimum-number-of-operations-to-sort-a-binary-tree-by-level/) | Medium | [C++](./old-problems/2471/solution.cpp) |
| [2482. Difference Between Ones and Zeros in Row and Column](https://leetcode.com/problems/difference-between-ones-and-zeros-in-row-and-column/) | Medium | [C](./old-problems/2482/c/solution.c) [C++](./old-problems/2482/solution.cpp) |
| [2485. Find the Pivot Integer](https://leetcode.com/problems/find-the-pivot-integer/) | Easy | [C](./old-problems/2485/c/solution.c) [C++](./old-problems/2485/solution.cpp) |
| [2486. Append Characters to String to Make Subsequence](https://leetcode.com/problems/append-characters-to-string-to-make-subsequence/) | Medium | [C](./old-problems/2486/c/solution.c) [C++](./old-problems/2486/solution.cpp) |
Expand Down
6 changes: 6 additions & 0 deletions old-problems/2471/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
get_dir_name(id)

add_executable(test-${id} test.cpp)
target_link_libraries(test-${id} PRIVATE Catch2::Catch2WithMain)

catch_discover_tests(test-${id})
6 changes: 6 additions & 0 deletions old-problems/2471/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
public:
int minimumOperations(TreeNode* root) {
return root->val;
}
};
64 changes: 64 additions & 0 deletions old-problems/2471/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;

TreeNode() : val{0}, left{nullptr}, right{nullptr} {}
TreeNode(int val) : val{val}, left{nullptr}, right{nullptr} {}
TreeNode(int val, TreeNode* left, TreeNode* right)
: val{val}, left{left}, right{right} {}

~TreeNode() {
if (left != nullptr) delete left;
if (right != nullptr) delete right;
}
};

// clang-format off
#include "solution.cpp"
// clang-format on

#include <catch2/catch_test_macros.hpp>
#include <optional>
#include <queue>
#include <vector>

TreeNode* to_tree(const std::vector<std::optional<int>>& list) {
if (list.empty()) return nullptr;
auto root{new TreeNode(*list[0])};

std::queue<TreeNode**> children{};
children.push(&(root->left));
children.push(&(root->right));

for (std::size_t i{1}; i < list.size(); ++i) {
if (list[i]) {
auto node{new TreeNode(*list[i])};
*children.front() = node;
children.push(&(node->left));
children.push(&(node->right));
}
children.pop();
}
return root;
}

TEST_CASE("2471. Minimum Number of Operations to Sort a Binary Tree by Level") {
SECTION("Example 1") {
const auto root = to_tree({1, 4, 3, 7, 6, 8, 5, std::nullopt, std::nullopt, std::nullopt, std::nullopt, 9, std::nullopt, 10});
REQUIRE(Solution{}.minimumOperations(root) == 3);
delete root;
}

SECTION("Example 2") {
const auto root = to_tree({1, 3, 2, 7, 6, 5, 4});
REQUIRE(Solution{}.minimumOperations(root) == 3);
delete root;
}

SECTION("Example 3") {
const auto root = to_tree({1, 2, 3, 4, 5, 6});
REQUIRE(Solution{}.minimumOperations(root) == 0);
delete root;
}
}
Loading