-
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
3 changed files
with
147 additions
and
1 deletion.
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
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; | ||
} | ||
|
||
} |
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,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 |