๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋ง๊ฐ๋กœ๊ทธ

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK5 Day37 - Binary Tree ๋ณธ๋ฌธ

Krafton jungle

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK5 Day37 - Binary Tree

habbn 2024. 2. 14. 00:52
728x90

๐Ÿ“†2024.2.13

 

์˜ค๋Š˜ Binary Tree ๊ณผ์ œ ๋‹ค ํ’€์—ˆ๋‹ค......

๋ชจ๋“  ๋ฌธ์ œ๊ฐ€ ์žฌ๊ท€๋กœ ํ‘ธ๋Š” ํ˜•์‹์ด๋ผ ์ ‘๊ทผ๋ฐฉ์‹์€ ๋‹ค ๋น„์Šทํ•ด์„œ ๊ธˆ๋ฐฉ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์–ด์ œ ์Šคํƒ ํ’€๋ฉด์„œ ๋ดค๋˜ ๊ด„ํ˜ธ ๋ฌธ์ œ ์ฐพ์•„์„œ ๋ฐฑ์ค€ ๊ด„ํ˜ธ ๋ฌธ์ œ ํ’€๊ณ , ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ฝ”๋”ฉ ๊ธฐ์ดˆ ํŠธ๋ ˆ์ด๋‹ ์บ˜๋ฆฐ๋” ๋„์žฅ ๊นจ๊ธฐ ํ•˜๊ณ  ์žˆ๋‹ค. ๋‚ด์ผ์€ BST ๋‹ค ํ’€๊ณ  ์ฑ… ,ํ‚ค์›Œ๋“œ ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค!

 

 

 

Binary Tree

identical

 

๋‘ ๊ฐœ์˜ ์ด์ง„ ํŠธ๋ฆฌ๊ฐ€ ๊ตฌ์กฐ์ ์œผ๋กœ ๋™์ผํ•œ์ง€ ํŒ๋ณ„

int identical(BTNode *tree1, BTNode *tree2)
{
   /* add your code here */
    if(tree1 == NULL && tree2 == NULL)          //๋‘ ํŠธ๋ฆฌ๊ฐ€ ๋ชจ๋“œ ๋น„์–ด์žˆ์œผ๋ฉด ๋™์ผํ•˜๋‹ค๊ณ  ํŒ๋‹จ
        return 1;
    else if(tree1 != NULL && tree2 != NULL){
        return(
            tree1->item == tree2->item &&      
            identical(tree1->left, tree2->left) &&
            identical(tree1->right,tree2->right)  
        );
    }
    else
        return 0;
}

 

maxHeight

 

์žฌ๊ท€ ์ด์šฉํ•ด์„œ ์ตœ๋Œ€ ๋†’์ด ๋‚˜ํƒ€๋ƒ„

int maxHeight(BTNode *node)
{
    /* add your code here */ 
    if(node == NULL)    return -1;
    else
    {
        int l = maxHeight(node->left);   
        int r = maxHeight(node->right);

        if(l > r)   return l + 1;
        else    return r + 1;
    }
}

 

countOneChildNodes

 

์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด์„œ ํ™€์ˆ˜์ธ ๋…ธ๋“œ์˜ ํ•ฉ์„ ๊ตฌํ•ด๋ผ

int sumOfOddNodes(BTNode *node)
{
    /* add your code here */
    if(node == NULL) return 0;
    else if(node->item % 2 == 1)
    {
        return sumOfOddNodes(node->left) + sumOfOddNodes(node->right) + node->item;
    }
    else    
        return sumOfOddNodes(node->left) + sumOfOddNodes(node->right);
}

 

 

mirrorTree

 

void mirrorTree(BTNode *node)
{
	/* add your code here */
    if(node == NULL)    return;
    else
    {
        //ํ˜„์žฌ ๋…ธ๋“œ์˜ ์™ผ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ์™€ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ์Šค์™‘
        BTNode *temp;
        temp = node->left;
        node->left = node->right;
        node->right = temp;

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

}

 

printSmallerValues

 

void printSmallerValues(BTNode *node, int m)
{
	/* add your code here */
    if(node == NULL)    return;
    else
    {
        if(node->item < m)
        {
            printf("%d ", node->item);
        }
        //์™ผ์ชฝ ์˜ค๋ฅธ์ชฝ ์„œ๋ธŒํŠธ๋ฆฌ ๋ชจ๋‘ ํƒ์ƒ‰
        printSmallerValues(node->left, m);      
        printSmallerValues(node->right,m);
    }
}

 

smallestValue

 

int smallestValue(BTNode *node)
{
	/* add your code here */
    if(node == NULL)   return -1;
    else if(node->left == NULL && node->right == NULL)
        return node->item;
    else
    {
        int leftsmall = smallestValue(node->left);
        int rightsmall = smallestValue(node->right);

        if(leftsmall > rightsmall) 
            return rightsmall;
        else
            return leftsmall;
    }
}

 

hasGreatGrandchild

 

int hasGreatGrandchild(BTNode *node)
{
	/* add your code here */
    if(node == NULL) return -1;
    else
    {
        int l = hasGreatGrandchild(node->left);
        int r = hasGreatGrandchild(node->right);   

        if(l + 1 > 2 || r + 1 > 2)          
            printf("%d ", node->item);
        return ((l > r) ? l : r ) + 1;
    }
}
728x90