【问题标题】:Root to leaf path sum = given number根到叶路径总和 = 给定数
【发布时间】:2020-03-23 18:28:12
【问题描述】:

这个递归是如何进行的?第一次是 14-10=4 和 if (node->left) 条件满足所以使用 node->left(node 8) 和 sum value(4) 的函数被调用,但是在 node->left 和 node->right 中 or 条件的用途是什么?

假设给定的总和是 21,那么在我们最后递归节点 3 并在 node->left 函数中调用 sum=3 后,1 作为 sum=0 返回,并且没有子节点,但是 1 返回的位置到节点 8,然后我们去节点 5?

如果我们做节点 5 不返回任何值,那么它如何计算返回 1 的左孩子,而它的右孩子不返回任何值?我看不到实际在哪里使用 or 条件,为什么在 node->left 和 node->right if 条件下都需要使用它?

int main()
{
  int sum=14; //sum=21;

  struct node *root = newnode(10);
  root->left        = newnode(8);
  root->right       = newnode(2);
  root->left->left  = newnode(3);
  root->left->right = newnode(5);
  root->right->left = newnode(2);

  if(hasPathSum(root, sum))
   printf("There is a root-to-leaf path with sum %d", sum);
  else
   printf("There is no root-to-leaf path with sum %d", sum);

   getchar();
   return 0;
}

bool hasPathSum(struct node* node, int sum)
 {
  /* return true if we run out of tree and sum==0 */
   if (node == NULL)
   {
      return (sum == 0);
   }

   else
  {
    bool ans = 0;     
    int subSum = sum - node->data;

    if ( subSum == 0 && node->left == NULL && node->right == NULL )
     return 1;

    if(node->left)
    ans = ans || hasPathSum(node->left, subSum);
    if(node->right)
    ans = ans || hasPathSum(node->right, subSum);

    return ans;
    }
}

【问题讨论】:

    标签: c binary-tree


    【解决方案1】:

    在:

    if(node->left)
        ans = ans || hasPathSum(node->left, subSum);
    if(node->right)
        ans = ans || hasPathSum(node->right, subSum);
    

    第一个“ans = ans || ...”没有任何功能,因为ans 是假的。在第二个 if 中, ans 可以被第一个 if 设置为 true,然后不会调用 hasPathSum。但是,它使代码看起来很好且易于阅读

    【讨论】:

      【解决方案2】:

      请参阅http://en.wikipedia.org/wiki/Short-circuit_evaluation,详细了解您的 || 中发生的事情。运算符。

      【讨论】:

        【解决方案3】:

        你可以使用

        if (root->left)
          ans = hasPathSum(root->left, subSum);
        if (root->right && ans ==  false)
          ans = hasPathSum(root->right, subSum);
        

        相反,也是正确的。

        正如 Paul 所说,如果第一个条件可以将 ans 设置为 true,则不再需要第二个条件和对递归方法的调用,因为已找到总和等于给定参数的路径。

        【讨论】:

          【解决方案4】:
          void path(Node *root,int arr[],int end){
               if(root==NULL)return;
               arr[end++]= root->data;
               if(root->left ==NULL && root->right==NULL){
                   for(int i = 0;i<end;i++){
                       cout<<arr[i]<<" ";
                   }
                   cout<<"#";
               }
               else{
                   if(root->left)path(root->left,arr,end);
                   if(root->right)path(root->right,arr,end);
               }
           }
          void printPaths(Node* root)
          {
              int pathArr[1000];
              path(root,pathArr,0);
              cout<<endl;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-09-11
            • 2019-05-17
            • 2016-08-03
            • 1970-01-01
            • 1970-01-01
            • 2015-02-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多