-
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.
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_catch(test_validate_binary_tree_nodes test.cpp) |
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,52 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
class DisjointSets { | ||
public: | ||
explicit DisjointSets(int size) : parents_(size), count_(size) { | ||
for (int i = 0; i < size; ++i) { | ||
parents_[i] = i; | ||
} | ||
} | ||
|
||
int Count() { return count_; } | ||
|
||
int Parent(int i) { | ||
if (parents_[i] != i) { | ||
parents_[i] = Parent(parents_[i]); | ||
} | ||
return parents_[i]; | ||
} | ||
|
||
bool Merge(int parent, int child) { | ||
if (child != Parent(child) || Parent(child) == Parent(parent)) { | ||
return false; | ||
} | ||
|
||
--count_; | ||
parents_[Parent(child)] = Parent(parent); | ||
return true; | ||
} | ||
|
||
private: | ||
std::vector<int> parents_; | ||
int count_; | ||
}; | ||
|
||
class Solution { | ||
public: | ||
static bool validateBinaryTreeNodes(int n, const std::vector<int> &left, | ||
const std::vector<int> &right) { | ||
DisjointSets sets(n); | ||
for (const auto &children : {left, right}) { | ||
for (int parent = 0; parent < n; ++parent) { | ||
const auto child = children[parent]; | ||
if (child != -1 && !sets.Merge(parent, child)) { | ||
return false; | ||
} | ||
} | ||
} | ||
return sets.Count() == 1; | ||
} | ||
}; |
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,45 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
int n; | ||
std::vector<int> leftChild; | ||
std::vector<int> rightChild; | ||
bool expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.n = 4, | ||
.leftChild{1, -1, 3, -1}, | ||
.rightChild{2, -1, -1, -1}, | ||
.expected = true, | ||
}, | ||
{ | ||
.n = 4, | ||
.leftChild{1, -1, 3, -1}, | ||
.rightChild{2, 3, -1, -1}, | ||
.expected = false, | ||
}, | ||
{ | ||
.n = 2, | ||
.leftChild{1, 0}, | ||
.rightChild{-1, -1}, | ||
.expected = false, | ||
}, | ||
{ | ||
.n = 4, | ||
.leftChild{1, 0, 3, -1}, | ||
.rightChild{-1, -1, -1, -1}, | ||
.expected = false, | ||
}, | ||
}; | ||
|
||
for (const auto &[n, leftChild, rightChild, expected] : test_cases) { | ||
const auto actual = | ||
Solution::validateBinaryTreeNodes(n, leftChild, rightChild); | ||
REQUIRE(expected == actual); | ||
} | ||
} |