LCP 67
This commit is contained in:
parent
cbd9bd31a9
commit
a89bec24e9
16
include/solution/lcp_67.h
Normal file
16
include/solution/lcp_67.h
Normal file
@ -0,0 +1,16 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/3/25.
|
||||
//
|
||||
|
||||
#ifndef LCP_67_H
|
||||
#define LCP_67_H
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
#include <TreeNode.h>
|
||||
struct TreeNode *expandBinaryTree(struct TreeNode *root);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // LCP_67_H
|
37
src/lcp_67.c
Normal file
37
src/lcp_67.c
Normal file
@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/3/25.
|
||||
//
|
||||
#include <solution/lcp_67.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void inorder(struct TreeNode *root)
|
||||
{
|
||||
if (root == NULL)
|
||||
return;
|
||||
inorder(root->left);
|
||||
if (root->left != NULL && root->val != -1)
|
||||
{
|
||||
struct TreeNode *p = root->left;
|
||||
root->left = malloc(sizeof(struct TreeNode));
|
||||
root->left->left = p;
|
||||
root->left->right = NULL;
|
||||
root->left->val = -1;
|
||||
}
|
||||
|
||||
if (root->right != NULL && root->val != -1)
|
||||
{
|
||||
struct TreeNode *p = root->right;
|
||||
root->right = malloc(sizeof(struct TreeNode));
|
||||
root->right->right = p;
|
||||
root->right->left = NULL;
|
||||
root->right->val = -1;
|
||||
}
|
||||
inorder(root->right);
|
||||
}
|
||||
|
||||
struct TreeNode *expandBinaryTree(struct TreeNode *root)
|
||||
{
|
||||
inorder(root);
|
||||
return root;
|
||||
}
|
54
tests/test_lcp_67.cpp
Normal file
54
tests/test_lcp_67.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
//
|
||||
// Created by xfj12 on 2025/3/25.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include <queue>
|
||||
#include <solution/lcp_67.h>
|
||||
|
||||
bool areTreesEqual(TreeNode *t1, TreeNode *t2)
|
||||
{
|
||||
if (!t1 && !t2)
|
||||
return true;
|
||||
if (!t1 || !t2)
|
||||
return false;
|
||||
return (t1->val == t2->val) && areTreesEqual(t1->left, t2->left) && areTreesEqual(t1->right, t2->right);
|
||||
}
|
||||
|
||||
TreeNode *buildTree(const std::vector<int> &values)
|
||||
{
|
||||
if (values.empty())
|
||||
return nullptr;
|
||||
std::vector<TreeNode *> nodes;
|
||||
for (int val : values)
|
||||
{
|
||||
nodes.push_back(val == INT_MIN ? nullptr : new TreeNode{val, nullptr, nullptr});
|
||||
}
|
||||
for (size_t i = 0, j = 1; j < nodes.size(); ++i)
|
||||
{
|
||||
if (nodes[i])
|
||||
{
|
||||
if (j < nodes.size())
|
||||
nodes[i]->left = nodes[j++];
|
||||
if (j < nodes.size())
|
||||
nodes[i]->right = nodes[j++];
|
||||
}
|
||||
}
|
||||
return nodes[0];
|
||||
}
|
||||
|
||||
TEST(ExpandBinaryTreeTest, Test1)
|
||||
{
|
||||
TreeNode *root = buildTree({7, 5, 6});
|
||||
TreeNode *expanded = expandBinaryTree(root);
|
||||
TreeNode *expected = buildTree({7, -1, -1, 5, INT_MIN, INT_MIN, 6});
|
||||
EXPECT_TRUE(areTreesEqual(expanded, expected));
|
||||
}
|
||||
|
||||
TEST(ExpandBinaryTreeTest, Test2)
|
||||
{
|
||||
TreeNode *root = buildTree({3, 1, 7, 3, 8, INT_MIN, 4});
|
||||
TreeNode *expanded = expandBinaryTree(root);
|
||||
TreeNode *expected =
|
||||
buildTree({3, -1, -1, 1, INT_MIN, INT_MIN, 7, -1, -1, INT_MIN, -1, 3, INT_MIN, INT_MIN, 8, INT_MIN, 4});
|
||||
EXPECT_TRUE(areTreesEqual(expanded, expected));
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user