크래프톤 정글 - TIL

크래프톤 정글 5기 TIL - Day 28(C언어 자료구조 - 이진 트리)

개발찾아 삼만리 2024. 4. 17. 23:43

1. 두 개의 트리 구조적 일치 확인하기(값, 구조 동일)

  • 트리의 노드를 비교해가면서 다르면 0을 반환
int identical(BTNode *tree1, BTNode *tree2)
{
    int result1, result2;
    if (tree1->item != tree2->item)
    {
        return 0;
    }

    if (tree1 == NULL && tree2 == NULL)
    {
        return 1;
    }

    result1 = identical(tree1->left, tree2->left);
    result2 = identical(tree1->right, tree2->right);
    if (result1 == result2)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

2. 트리의 최대 높이 구하기

  • 노드가 없을 경우에는 1 (노드가 1개라도 존재하면 높이가 0)
  • 오른쪽, 왼쪽 노드의 최대 높이를 비교해서 최대 높이 추출
int maxHeight(BTNode *node)
{
    int left_depth, right_depth;
    if (node == NULL)
    {
        return -1;
    }

    left_depth = maxHeight(node->left);
    left_depth++;
    right_depth = maxHeight(node->right);
    right_depth++;

    if (left_depth > right_depth)
    {
        return left_depth;
    }
    else
    {
        return right_depth;
    }
}

3. 자식 노드를 한 개만 가진 노드 개수 세기

  • 왼쪽 노드만 있거나, 오른쪽 노드만 있는 노드의 개수를 카운트
int countOneChildNodes(BTNode *node)
{
    int count = 0;
    if (node == NULL)
    {
        return 0;
    }

    count += countOneChildNodes(node->left);
    count += countOneChildNodes(node->right);
    if (node->left != NULL && node->right == NULL)
    {
        count++;
    }
    else if (node->right != NULL && node->left == NULL)
    {
        count++;
    }
    return count;
}

4. 홀수인 노드의 합

  • 노드가 홀수인 것만 결과값에 더해서 반환
int sumOfOddNodes(BTNode *node)

{
    int result = 0;
    if (node == NULL)
    {
        return 0;
    }

    result += sumOfOddNodes(node->left);
    result += sumOfOddNodes(node->right);

    if (node->item % 2 == 1)
    {
        result += node->item;
    }
    return result;
}

5. 트리 좌우 뒤집기

  • 리프 노드부터 차례대로 순차적으로 바꿈
void mirrorTree(BTNode *node)
{
    BTNode *temp;
    if (node == NULL)
    {
        return;
    }

    mirrorTree(node->left);
    mirrorTree(node->right);
    temp = node->left;
    node->left = node->right;
    node->right = temp;
}

6. 둘 중 작은 노드의 값 출력하기

  • 왼쪽 자식 노드와 오른쪽 자식 노드를 비교해서 작은 값을 출력
void printSmallerValues(BTNode *node, int m)
{
    if (node == NULL)
    {
        return;
    }
    printSmallerValues(node->left, m);
    printSmallerValues(node->right, m);
    if (node->item < m)
    {
        printf("%d ", node->item);
    }
}

7. 가장 작은 노드의 값 출력하기

  • 노드를 탐색하면서 최소 값을 갱신
int smallestValue(BTNode *node)
{
    int min = __INT32_MAX__;
    int result1, result2;
    if (node == NULL)
    {
        return min;
    }
    result1 = smallestValue(node->left);
    result2 = smallestValue(node->right);
    if (result1 > result2)
    {
        min = result2;
    }
    else
    {
        min = result1;
    }
    if (min > node->item)
    {
        min = node->item;
        return min;
    }
    else
    {
        return min;
    }
}

8. 증손자 노드를 가진 노드 찾기(깊이가 3인 노드)

  • 이전에 했던 높이를 측정하는 문제와 동일하게 왼쪽 노드, 오른쪽 노드 깊이를 측정해서 깊이가 3 이상인 노드들을 출력
int hasGreatGrandchild(BTNode *node)
{
    int left_depth, right_depth;
    if (node == NULL)
    {
        return -1;
    }

    left_depth = hasGreatGrandchild(node->left);
    left_depth++;
    right_depth = hasGreatGrandchild(node->right);
    right_depth++;
    if (left_depth >= 3 || right_depth >= 3)
    {
        printf("%d\n", node->item);
    }
    if (left_depth > right_depth)
    {
        return left_depth;
    }
    else
    {
        return right_depth;
    }
}