如何在 DOMJUDGE 中找到问题运行错误

问题描述 投票:0回答:0

我向 DOMJUDGE 提交了一个关于 AVL 的问题,结果是正确的,运行时间为 0 秒,但它的 RUN-ERROR

这是我的程序,你能帮我找出问题吗? 它是关于将值的父级与值父级的兄弟相加,例如,如果值是 17 和 17,父级是 29,而 29 有 15 作为它的兄弟姐妹,所以它将 29+15

int height(AVLNode *root)
{
      if (root == NULL)
            return 0;
      else
      {
            int lheight = height(root->left);
            int rheight = height(root->right);
            if (lheight > rheight)
                  return (lheight + 1);
            else
                  return (rheight + 1);
      }
}

bool CurrentLevel(AVLNode *root, int level, int val)
{
      if (root == NULL)
            return false;

      if (level == 1)
      {
            if (root->data == val)
            {
                  return true;
            }
      }

      else if (level > 1)
      {
            bool left = CurrentLevel(root->left, level - 1, val);
            bool right = CurrentLevel(root->right, level - 1, val);
            return left || right;
      }
      return false;
}

AVLNode *findGrandparent(AVLNode *root, int val)
{
      if (root == NULL || (root->left == NULL && root->right == NULL))
      {
            return NULL;
      }
      else if ((root->left != NULL && (root->left->left != NULL || root->left->right != NULL) && (root->left->left->data == val || root->left->right->data == val)) || (root->right != NULL && (root->right->left != NULL || root->right->right != NULL) && (root->right->left->data == val || root->right->right->data == val)))
      {
            return root;
      }
      else
      {
            AVLNode *left = findGrandparent(root->left, val);
            if (left != NULL)
            {
                  return left;
            }
            else
            {
                  AVLNode *right = findGrandparent(root->right, val);
                  if (right != NULL)
                  {
                        return right;
                  }
                  else
                  {
                        return NULL;
                  }
            }
      }
}

int count_parent(AVLNode *root, int level, int val)
{
      if (root == NULL || level < 2)
      {
            return 0;
      }

      if (level == 2)
      {
            if (root->left != NULL && root->left->data == val)
            {
                  return root->data;
            }
            else if (root->right != NULL && root->right->data == val)
            {
                  return root->data;
            }
      }

      if (level >= 3)
      {
            AVLNode *grandparent = findGrandparent(root, val);
            if (grandparent != NULL)
            {
                  int left_child = (grandparent->left != NULL) ? grandparent->left->data : 0;
                  int right_child = (grandparent->right != NULL) ? grandparent->right->data : 0;
                  return left_child + right_child;
            }
      }

      int left_sum = count_parent(root->left, level - 1, val);
      int right_sum = count_parent(root->right, level - 1, val);
      return left_sum + right_sum;
}


int LOT(AVL *root, int value)
{
      int h = height(root->_root);
      int i;
      for (i = 1; i <= h; i++)
      {
            if (CurrentLevel(root->_root, i, value))
            {
                 return count_parent(root->_root, i, value);
            }
      }

      return 0;
}

int main()
{
      AVL avlku;
      avl_init(&avlku);

      int n,m,val,srch,x;

      scanf("%d", &n);
      scanf("%d", &m);

      int i;
      for (i = 0; i < n; i++)
      {
            scanf("%d", &val);
            avl_insert(&avlku, val);
      }

      for (i = 0; i < m; i++)
      {
            scanf("%d", &srch);
            x = LOT(&avlku, srch);
            printf("%d\n", x);
      }

      return 0;

}
c++ c avl-tree
© www.soinside.com 2019 - 2024. All rights reserved.