Skip to content

Commit

Permalink
feat: update lc-1361
Browse files Browse the repository at this point in the history
Signed-off-by: Certseeds <51754303+Certseeds@users.noreply.github.com>
  • Loading branch information
Certseeds committed Aug 26, 2023
1 parent 8b30e8f commit 4e8a333
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
2 changes: 1 addition & 1 deletion algorithm/disjoint_set/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ add_library(${PROJECT_NAME}_lib ${LIB_WAY} ${CMAKE_CURRENT_SOURCE_DIR}/disjoint_
target_link_libraries(${PROJECT_NAME}_lib PRIVATE CS203_DSAA_template_INCLUDE)

enable_testing()
set(dependencies 684 200 130 399)
set(dependencies 684 200 130 399 1361)
LIST(TRANSFORM dependencies PREPEND leetcode_)

LIST(APPEND dependencies disjoint_set_lnkedlist)
Expand Down
78 changes: 78 additions & 0 deletions algorithm/disjoint_set/leetcode_1361.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template
Copyright (C) 2020-2023 nanos
*/
#include "leetcode_1361_test.hpp"
#include "disjoint_set.hpp"
#include <queue>

namespace leetcode_1361 {
using ::disjoint_set::disjoint_set;
using ::disjoint_set::getDisjointSet;

// 二叉树上x个节点应该有x-1条边
bool simpleCheck(int32_t n, const vector<int32_t> &leftChild, const vector<int32_t> &rightChild) {
int32_t count{0};
for (int32_t i{0}; i < n; i++) {
count += (leftChild[i] != -1) + (rightChild[i] != -1);
}
return (n == count + 1);
}

bool
leetcode_1361::validateBinaryTreeNodes(int32_t n, const vector<int32_t> &leftChild, const vector<int32_t> &rightChild) {
if (!simpleCheck(n, leftChild, rightChild)) {
return false;
}
vector<int32_t> in_position(n, -1);
for (int32_t i{0}; i < n; i++) {
if (leftChild[i] != -1) {
if (in_position[leftChild[i]] == -1) {
in_position[leftChild[i]] = i;
} else {
return false;
}
}
if (rightChild[i] != -1) {
if (in_position[rightChild[i]] != -1) {
return false;
}
in_position[rightChild[i]] = i;
}
} // 父节点不能有多个
// O(n)
int32_t count{-1};
for (int32_t i{0}; i < n; i++) {
if (in_position[i] == -1) {
if (count != -1) {// 循环节点不会被扫描到, 因为它们有子节点也有父节点
return false;
}
count = i;
}
}// 只能有一个根节点
// count must be root
// O(n)
int32_t visit_nums{0};
for (std::queue<int32_t> que{{count}}; !que.empty();) {
const auto head = que.front();
que.pop();
visit_nums += 1;
if (leftChild[head] != -1) {
que.push(leftChild[head]);
}
if (rightChild[head] != -1) {
que.push(rightChild[head]);
}
}
// O(n)
//根节点遍历必须得能遍历到所有节点
if (visit_nums != n) {
return false;
}
return true;
}

}
68 changes: 68 additions & 0 deletions algorithm/disjoint_set/leetcode_1361_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
/*
CS203_DSAA_template
Copyright (C) 2023 nanos
*/
//@Tag disjoint_set
//@Tag 并查集
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_DP_LEETCODE_1361_TEST_CPP
#define CS203_DSAA_TEMPLATE_ALGORITHM_DP_LEETCODE_1361_TEST_CPP

#include <catch_main.hpp>
#include <cstdint>
#include <cstddef>
#include <vector>

namespace leetcode_1361 {
using std::vector;

namespace leetcode_1361 {
bool validateBinaryTreeNodes(int32_t n, const vector<int32_t> &leftChild, const vector<int32_t> &rightChild);

}

TEST_CASE("1 [test_1361]", "[test_1361]") {
constexpr const auto n{4};
const vector<int32_t> left{1, -1, 3, -1}, right{2, -1, -1, -1};
CHECK(leetcode_1361::validateBinaryTreeNodes(n, left, right));
}

TEST_CASE("2 [test_1361]", "[test_1361]") {
constexpr const auto n{4};
const vector<int32_t> left{1, -1, 3, -1}, right{2, 3, -1, -1};
CHECK_FALSE(leetcode_1361::validateBinaryTreeNodes(n, left, right));
}

TEST_CASE("3 [test_1361]", "[test_1361]") {
constexpr const auto n{2};
const vector<int32_t> left{1, 0}, right{-1, -1};
CHECK_FALSE(leetcode_1361::validateBinaryTreeNodes(n, left, right));
}

TEST_CASE("4 [test_1361]", "[test_1361]") {
constexpr const auto n{6};
const vector<int32_t> left{1, -1, -1, 4, -1, -1}, right{2, -1, -1, 5, -1, -1};
CHECK_FALSE(leetcode_1361::validateBinaryTreeNodes(n, left, right));
}

TEST_CASE("5 [test_1361]", "[test_1361]") {
constexpr const auto n{3};
const vector<int32_t> left{1, -1, -1}, right{-1, -1, 1};
CHECK_FALSE(leetcode_1361::validateBinaryTreeNodes(n, left, right));
}

TEST_CASE("6 [test_1361]", "[test_1361]") {
constexpr const auto n{4};
const vector<int32_t> left{3, -1, 1, -1}, right{-1, -1, 0, -1};
CHECK(leetcode_1361::validateBinaryTreeNodes(n, left, right));
}

TEST_CASE("7 [test_1361]", "[test_1361]") {
constexpr const auto n{4};
const vector<int32_t> left{1, 0, 3, -1}, right{-1, -1, -1, -1};
CHECK_FALSE(leetcode_1361::validateBinaryTreeNodes(n, left, right));
}
}
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_DP_LEETCODE_1361_TEST_CPP

0 comments on commit 4e8a333

Please sign in to comment.