-
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
6 changed files
with
75 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
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_add_one_row_to_tree 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,23 @@ | ||
#pragma once | ||
|
||
#include <tree_node.h> | ||
|
||
// Time: O(N) | ||
// Space: O(N) | ||
|
||
class Solution { | ||
public: | ||
static TreeNode *addOneRow(TreeNode *root, int val, int depth, | ||
bool left = true) { | ||
if (depth == 1) { | ||
return left ? new TreeNode(val, root, nullptr) | ||
: new TreeNode(val, nullptr, root); | ||
} | ||
if (!root) { | ||
return nullptr; | ||
} | ||
root->left = addOneRow(root->left, val, depth - 1, true); | ||
root->right = addOneRow(root->right, val, depth - 1, false); | ||
return root; | ||
} | ||
}; |
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,48 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
#include <tree_node.h> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
Tree root; | ||
int val; | ||
int depth; | ||
Tree expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.root = {4, 2, 6, 3, 1, 5}, | ||
.val = 1, | ||
.depth = 2, | ||
.expected = {4, 1, 1, 2, std::nullopt, std::nullopt, 6, 3, 1, 5}, | ||
}, | ||
{ | ||
.root = {4, 2, std::nullopt, 3, 1}, | ||
.val = 1, | ||
.depth = 3, | ||
.expected = {4, 2, std::nullopt, 1, 1, 3, std::nullopt, std::nullopt, | ||
1}, | ||
}, | ||
{ | ||
.root = {5}, | ||
.val = 1, | ||
.depth = 1, | ||
.expected = {1, 5}, | ||
}, | ||
{ | ||
.root = {1, 2, 3, 4}, | ||
.val = 5, | ||
.depth = 4, | ||
.expected = {1, 2, 3, 4, std::nullopt, std::nullopt, std::nullopt, 5, | ||
5}, | ||
}, | ||
}; | ||
|
||
for (const auto &[root, val, depth, expected] : test_cases) { | ||
const Tree actual = Solution::addOneRow(Copy(root), val, depth); | ||
REQUIRE(expected == actual); | ||
} | ||
} |