首页 > 解决方案 > 有人可以告诉我的树功能有什么问题吗

问题描述

我有一棵树,我需要编写一个函数,在每个偶数根层中它必须包含一个偶数值,并且在每个奇数根层中它必须包含奇数值,左侧的奇数值也必须是该值的 3 次幂( value^3) 如果语句正确,则右侧奇数值和右侧偶数值的奇数值应大于左侧偶数值,则该函数应返回 1,否则返回 0;

在代码下方会有一张我的意思的图片。

这是我的代码:

    typedef struct node
    {
        int value;
        struct node *left;
        struct node *right;
    } node;
     
int main()
{
    node *root;
    int even_odd=0;
    printf("create a tree:\n");
    root=create();/*the function to get the user input*/
    i=0;
    even_odd=evenodd(root,0);
   
   if ( even_odd)
    {
        printf("the tree is  an amazing tree\n");
    }
   
    return 0;
}

    int evenodd(node *root, int n)
    {
         int check1=0,check2=0,counter=0,true1=0,true2=0;
         if(root == NULL)
         {
             return 0;
         }
         else if(root->left == NULL && root->right == NULL)
         {
             return 0;
         }
         else
         {
             if (root->right != NULL && root->right && root->left)
             {
                 counter++;
                 check1=evenodd(root->left,root->value);
                 check2=evenodd(root->right,root->value);
                 if (counter % 2 == 0)
                 {
                     if ((check2 %2==0 && check1%2==0) && check2>check1)
                     {
                         true1=1;
                     }
                 }
                 else if (counter %2 != 0)
                 {
                     if ((check2 %2!=0 && check1%2!=0) && pow(check2,3) > check1)
                     {
                         true2=1;
                     }
                 }
                 return (true1 && true2); /*the function returns 1*/
             }
         }
     }

树

标签: c

解决方案


推荐阅读