Skip to content

Commit

Permalink
feat: update lc-1372
Browse files Browse the repository at this point in the history
find something amazing

Signed-off-by: Certseeds <[email protected]>
  • Loading branch information
Certseeds committed Sep 3, 2023
1 parent a676432 commit 7d74146
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
2 changes: 1 addition & 1 deletion algorithm/tree/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LIST(APPEND leetcode_order 113 426 so_54 114 230)
LIST(APPEND leetcode_order 236 543 199 814 872)
LIST(APPEND leetcode_order 894 897 938 965 993)
LIST(APPEND leetcode_order 1008 1022 1038 1104 1261)
LIST(APPEND leetcode_order 1302 1315 1325 1367)
LIST(APPEND leetcode_order 1302 1315 1325 1367 1372)
LIST(TRANSFORM leetcode_order PREPEND leetcode_)

set(dependencies ${dependencies} ${leetcode_order})
Expand Down
57 changes: 57 additions & 0 deletions algorithm/tree/leetcode_1372.cpp
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;
}

}
48 changes: 48 additions & 0 deletions algorithm/tree/leetcode_1372_test.hpp
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

0 comments on commit 7d74146

Please sign in to comment.