在 c++ 中使用递归返回一个值

发布于 2024-12-11 06:04:12 字数 506 浏览 0 评论 0原文

我接受了采访,无法回答这个问题。您有一个二叉树,并且使用 in_order 将所有节点放入数组中,然后返回数组大小的值。有人告诉我不能使用辅助函数并为数组计数器添加 int i=0 。

我必须使用的递归函数标题是。

      In_order(Struct_Node * node, int *array){

      }

因为我是按顺序写的。

      if(node){
      In_order(node->left, array);

//这一行是我应该添加元素并返回值的地方, 但我不明白该怎么做。这是我的一周重点,我需要理解 这段代码如何工作的推理比代码是什么更重要。

      in_order(node->right,array); 
      }

我实际上并没有写在两个 In_order 语句之间写的内容,但我写的内容是错误的。

I had an interview and I could not answer this question. You have an binary tree and you are putting all the nodes into an array using in_order and you return the value for the size of the array. I was told that I could not use a helper function and add int i=0 for an array counter.

The recursive function heading I had to use was.

      In_order(Struct_Node * node, int *array){

      }

Because it was In_order I wrote.

      if(node){
      In_order(node->left, array);

//this line is where I am supposed to add the elements and return the value,
but I do not understand how to do this. This is my week point and I need to understand
the reasoning of how this code works more than what is the code.

      in_order(node->right,array); 
      }

I didn't actually write what I wrote in between the two In_order statements but what I wrote was wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽你眉间 2024-12-18 06:04:13

猜测所有丢失的细节,我认为你会做类似的事情:

int In_order(Struct_Node * node, int *array){

   int count = 0;
   if (node->left) {
     count += In_order(node->left, array);
   }
   array[count++] = node->data; // whatever it is you're storing
   if (node->right) {
     count += In_order(node->right, array+count);
   }
   return count;
}

关键点是你将更新的指针传递给 RHS 递归调用,并且你的返回值为 1 + LHS return + RHS return

如果你可以'不要使用 int 作为计数器(这真的很愚蠢,因为它是迄今为止最清晰的表达方式),你可以这样做:

int In_order(Struct_Node * node, const int *array){
   int *tptr = array;

   if (node->left) {
     tptr += In_order(node->left, array);
   }
   *tptr++ = node->data; // whatever it is you're storing
   if (node->right) {
     tptr += In_order(node->right, tptr);
   }

   return tptr-array;
}

如果你想“有点”疯狂并避免除函数参数之外的任何局部变量,你可以更改返回类型为指向数组当前工作端的指针(实际上也是数组的大小)并执行以下操作:

int *In_order(const Struct_Node * const node, int * const result) {
  return !node ? result :
          In_order(node->right, &(*In_order(node->left, result) = node->value)+1);
}

尽管说​​实话,这是糟糕的代码(我什至不能 100% 确定它定义良好!)并且我真的希望他们没有在寻找这样的解决方案!

Guessing all the missing details I think you would do something like:

int In_order(Struct_Node * node, int *array){

   int count = 0;
   if (node->left) {
     count += In_order(node->left, array);
   }
   array[count++] = node->data; // whatever it is you're storing
   if (node->right) {
     count += In_order(node->right, array+count);
   }
   return count;
}

The key point is you pass an updated pointer to the RHS recursive call and your return value is 1 + LHS return + RHS return

If you can't use an int as a counter (which is silly really because it's by far the clearest way of expressing it) you can do:

int In_order(Struct_Node * node, const int *array){
   int *tptr = array;

   if (node->left) {
     tptr += In_order(node->left, array);
   }
   *tptr++ = node->data; // whatever it is you're storing
   if (node->right) {
     tptr += In_order(node->right, tptr);
   }

   return tptr-array;
}

If you wanted to go "a little" crazy and avoid any locals other than the function arguments you could change the return type to be a pointer to the current working end of the array (which is in effect the size of the array also) and do:

int *In_order(const Struct_Node * const node, int * const result) {
  return !node ? result :
          In_order(node->right, &(*In_order(node->left, result) = node->value)+1);
}

Although to be quite honest that's terrible code (I'm not even 100% sure that it is well defined!) and I really hope they weren't looking for a solution like that!

又怨 2024-12-18 06:04:13

中序是树遍历的一种方法;您按顺序(因此得名)从左到右移动节点。

因此,对于插入部分,您可以将所有节点插入当前节点的左侧,插入当前节点,然后将所有节点插入右侧。

in_order(node->right,array) 调用之后,您返回您的值(您的问题描述不清楚您应该返回什么)。

In-order is a method of tree traversal; you travel the nodes in-order (hence the name) from left to right.

So for the insertion part, you would insert all the nodes to the left of the current node, insert the current node, and then insert all the nodes to the right.

After the in_order(node->right,array) call, you return your value (your problem description is unclear on exactly what you're supposed to be returning).

箹锭⒈辈孓 2024-12-18 06:04:13

对于中序遍历:

  1. 对左子树(如果有)进行中序遍历。
  2. 访问当前节点。
  3. 对右子树(如果有)进行中序遍历。

在您的情况下,“访问”意味着将当前节点添加到列表中,并增加计数器。

至于将元素放入数组中,就我个人而言,我会使用向量(如果绝对必要,然后在最后转换/复制到数组中)。您可以在“启动”函数中创建它,然后调用递归函数,或者在调用方中创建它。无论哪种方式,您都可以在每次调用中传递对它的引用。最后,您将获得节点值数组和计数。否则,您最终必须遍历树一次来计算元素,然后再遍历一次来收集它们。 (或者你可以传递一个引用或指向一个指针的指针,这样你就可以根据需要重新分配,但这会变得很难看。无论哪种方式,我都不相信调用者知道给你一个多大的数组——这是你的工作知道/计算你的树的大小:P)

For an inorder traversal:

  1. Do an inorder traversal of the left subtree (if there is one).
  2. Visit the current node.
  3. Do an inorder traversal of the right subtree (if there is one).

In your case, "visit" means to add the current node to the list, and bump the counter.

As for putting the elements into an array, personally, i'd use a vector (and then convert/copy to an array at the end, if absolutely necessary). You can create it in a "start" function that then calls the recursive one, or create it in the caller. Either way, you pass a reference to it along in each call. At the end, you have both an array of node values, and the count. Otherwise you end up having to go through the tree once to count elements, and a second time to collect them. (Or you could pass a reference or pointer to a pointer, so you can reallocate as needed, but that gets ugly. Either way, i wouldn't trust the caller to know how big an array to give you -- it's your job to know/figure the size of your tree. :P )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文