-
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.
find something amazing Signed-off-by: Certseeds <[email protected]>
- Loading branch information
Showing
3 changed files
with
106 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,57 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022 nanoseeds | ||
*/ | ||
#include "leetcode_1372_test.hpp" | ||
#include <queue> | ||
|
||
namespace leetcode_1372 { | ||
using std::queue; | ||
|
||
int32_t longestZigZagLeft(TreeNode *root); | ||
|
||
int32_t longestZigZagRight(TreeNode *root); | ||
|
||
int32_t longestZigZagLeft(TreeNode *root) { | ||
if (root->right == nullptr) { | ||
return 0; | ||
} | ||
return longestZigZagRight(root->right) + 1; | ||
} | ||
|
||
int32_t longestZigZagRight(TreeNode *root) { | ||
if (root->left == nullptr) { | ||
return 0; | ||
} | ||
return longestZigZagLeft(root->left) + 1; | ||
} | ||
|
||
int32_t leetcode_1372::longestZigZag(TreeNode *root) { | ||
if (root == nullptr) { | ||
return 0; | ||
} | ||
int32_t maximum{longestZigZagLeft(root)}; // so we can left the other special case. | ||
for (queue<std::pair<TreeNode *, bool>> que{{{root, true}}}; !que.empty();) { | ||
const auto [head, dire] {que.front()}; | ||
que.pop(); | ||
if (dire) { | ||
const auto v = longestZigZagRight(head); | ||
maximum = std::max(maximum, v); | ||
} else { | ||
const auto v = longestZigZagLeft(head); | ||
maximum = std::max(maximum, v); | ||
} | ||
if (head->left != nullptr) { | ||
que.push({head->left, true}); | ||
} | ||
if (head->right != nullptr) { | ||
que.push({head->right, false}); | ||
} | ||
} | ||
return maximum; | ||
} | ||
|
||
} |
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 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
/* | ||
CS203_DSAA_template | ||
Copyright (C) 2022-2023 nanoseeds | ||
*/ | ||
//@Tag tree | ||
//@Tag 树 | ||
#ifndef CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_1372_TEST_HPP | ||
#define CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_1372_TEST_HPP | ||
|
||
#include <catch_main.hpp> | ||
#include <cstdint> | ||
#include <cstddef> | ||
#include <tree/treenode.hpp> | ||
#include <tree/treenode_link.hpp> | ||
|
||
namespace leetcode_1372 { | ||
using TreeNode = TREE_NODE::TreeNode<int32_t>; | ||
|
||
|
||
namespace leetcode_1372 { | ||
int32_t longestZigZag(TreeNode *root); | ||
} | ||
|
||
using TreeNodeLink = TREE_NODE::TreeNodeLink<int32_t>; | ||
|
||
TEST_CASE("test_case 1 [test_1372]", "[test_1372]") { | ||
const TreeNodeLink vec2{ | ||
1, | ||
TreeNode::No, 1, | ||
TreeNode::No, TreeNode::No, 1, 1, | ||
TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, TreeNode::No, 1, 1, | ||
}; | ||
TreeNode node2{1}, node3{1}; | ||
vec2[13]->right = &node2; | ||
node2.right = &node3; | ||
CHECK(3 == leetcode_1372::longestZigZag(vec2[0])); | ||
} | ||
|
||
TEST_CASE("test_case 2 [test_1372]", "[test_1372]") { | ||
const TreeNodeLink vec2{1}; | ||
CHECK(0 == leetcode_1372::longestZigZag(vec2[0])); | ||
} | ||
|
||
} | ||
#endif //CS203_DSAA_TEMPLATE_ALGORITHM_TREE_LEETCODE_1372_TEST_HPP |