距离函数中的数值是否被保留?

huangapple go评论67阅读模式
英文:

Is the value in the distance function being retained?

问题

我试图找到两个节点之间的距离。
在我的具体情况下,两个节点之间的距离是节点之间的边数。

#include <iostream>

using namespace bhargav;

int distance(TreeNode* root, TreeNode* n1)
{
   static int height = -1;
   
   if (root == NULL)
      return -1;

   if (root == n1)
      return ++height;  

   int left = distance(root->left, n1);
   
   if (left >= 0)
      return ++height;

   int right = distance(root->right, n1);
   
   if (right >= 0)
      return ++height;

   return height;
}

int main()
{
   TreeNode* root = new TreeNode(1);
   root->left = new TreeNode(2);
   root->right = new TreeNode(3);
   root->right->left = new TreeNode(4);
   root->right->right = new TreeNode(5);
   std::cout << distance(root->right, root->right->left) << "\n";
   
   std::cout << distance(root->right, root->right->right) << "\n";
   
}
  1. 如果我在主函数中调用语句
    std::cout << distance(root->right, root->right->left) << "\n"; // 1
    我得到1作为输出。

  2. 如果我调用语句
    std::cout << distance(root->right, root->right->right) << "\n"; // 1
    在主函数中,我得到1作为输出。

  3. 但是,如果我调用这两个语句

std::cout << distance(root->right, root->right->left) << "\n"; // 1
std::cout << distance(root->right, root->right->right) << "\n"; // 2

我得到1和2作为输出,我想找出输出变化的根本原因。

英文:

I am trying to find the distance between two nodes.
In my specific case, the distance between two nodes is the no of edges between the nodes.

#include &lt;iostream&gt;

using namespace bhargav;


int distance(TreeNode* root, TreeNode* n1)
{
   static int height = -1;
   
   if (root == NULL)
      return -1;

   if (root == n1)
      return ++height;  

   int left = distance(root-&gt;left, n1);
   
   if (left &gt;= 0)
      return ++height;

   int right = distance(root-&gt;right, n1);
   
   if (right &gt;= 0)
      return ++height;

   return height;
}

int main()
{
   TreeNode* root = new TreeNode(1);
   root-&gt;left = new TreeNode(2);
   root-&gt;right = new TreeNode(3);
   root-&gt;right-&gt;left = new TreeNode(4);
   root-&gt;right-&gt;right = new TreeNode(5);
   std::cout &lt;&lt; distance(root-&gt;right, root-&gt;right-&gt;left) &lt;&lt; &quot;\n&quot;;
   
   std::cout &lt;&lt; distance(root-&gt;right, root-&gt;right-&gt;right) &lt;&lt; &quot;\n&quot;;
   
}

  1. If I call the statement
    std::cout &lt;&lt; distance(root-&gt;right, root-&gt;right-&gt;left) &lt;&lt; &quot;\n&quot;; // 1
    in the main function, I get 1 as output.

  2. If I call the statement
    std::cout &lt;&lt; distance(root-&gt;right, root-&gt;right-&gt;right) &lt;&lt; &quot;\n&quot;; // 1
    in the main function, I get 1 as output.

  3. However f I call both statements

std::cout &lt;&lt; distance(root-&gt;right, root-&gt;right-&gt;left) &lt;&lt; &quot;\n&quot;; // 1
std::cout &lt;&lt; distance(root-&gt;right, root-&gt;right-&gt;right) &lt;&lt; &quot;\n&quot;; // 2

I get 1 and 2 as output, I want to find the underlying cause of the change in output.

答案1

得分: 3

一个static局部变量将在所有对函数的调用之间共享。它只会被初始化一次,而且只会初始化一次。你不能调用函数两次并期望函数会再次初始化它。

实际上,从编译器的角度来看,对函数的调用没有任何区别:函数调用就是函数调用就是函数调用... 递归对编译器来说并不是什么重要的事情,编译器不知道也不关心,一切都只是相同的调用。

要解决distance无法多次调用(从"顶层")的问题,你可以使用一个辅助函数来执行实际的工作,以及一个distance函数,它将height定义为一个普通的非静态变量,对其进行初始化,并将其作为参数传递给辅助函数。

也许可以像这样做:

int distance_helper(TreeNode* root, TreeNode* n1, int& height)
{
    // ...
    int left = distance_helper(root->left, n1, height);
    // ...
}

int distance(TreeNode* root, TreeNode* n1)
{
    int height = -1;
    return distance_helper(root, n1, height);
}
英文:

A static local variable will be shared between all calls to the function. It's initialized once, and only once. You can't call the function twice and expect the function to initialize it again.

In fact, from the compilers point of view there's really no difference between calls to a function: A function call is a function call is a function call... Recursion is not something the compiler knows or cares about, it's all just the same call.

To solve the problem of distance not being possible to call multiple times (from the "top" level), you can use a helper function to do the actual work, and a distance function which defines height as a normal non-static variable, initializes it, and passes it as an argument to the helper function.

Perhaps something like this:

int distance_helper(TreeNode* root, TreeNode* n1, int&amp; height)
{
    // ...
    int left = distance_helper(root-&gt;left, n1, height);
    // ...
}

int distance(TreeNode* root, TreeNode* n1)
{
    int height = -1;
    return distance_helper(root, n1, height);
}

huangapple
  • 本文由 发表于 2023年7月12日 22:56:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定