//
// Created by xfj12 on 2025/3/25.
//
#include <climits>
#include <gtest/gtest.h>
#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));
}