【问题标题】:How to save a value to increment in a recursive function (Javascript)如何保存一个值以在递归函数中递增(Javascript)
【发布时间】:2018-02-13 06:50:19
【问题描述】:
var count = 0;
var NumbOfNodes = function(root){

   if(root.left != null){
       NumbOfNodes(root.left);
   }

   count++;

   if(root.right != null){
       NumbOfNodes(root.right);
   }

   return count;
}

所以我有这个函数来计算树中的节点数。它是一个递归函数。当我计数一个全局变量时,该函数工作并返回一个正确的值。问题是,我希望 count 成为函数的一部分。但是如果我在函数中声明 count ,它会不断被重置,我不会从函数中得到正确的答案。

那么,有没有办法在递归函数中保留变量的值。这个问题可以帮助我进行许多其他编码实践。

我想要的(图表):Tree --> NumbOfNodes() --> 给了我节点的数量。但我不想只为它创建一个全局变量。

谢谢!

【问题讨论】:

  • 您可以在函数中添加一个额外的 int 参数,始终传入 0 以启动。然后像往常一样在函数中递增以计算节点并返回它。

标签: javascript recursion


【解决方案1】:

只需从递归中返回计数:

var NumbOfNodes = function(root) {
    var count = 0;
    if (root.left != null) {
        count += NumbOfNodes(root.left);
    }

    count++;     // count ourself

    if (root.right != null) {
        count += NumbOfNodes(root.right);
    }

    return count;
}

这里的基本思想是,在递归的每一步,我们从零开始。然后我们将左子树和右子树中的节点数添加到该计数中,并且我们添加一个,以计算我们自己。

【讨论】:

  • 嗯,这是迄今为止最好的解决方案,非常感谢!
【解决方案2】:

您可以将count 作为参数传递给该函数,例如

var NumbOfNodes = function(root, count){
   if(!count) count = 0;

   if(root.left != null){
       NumbOfNodes(root.left, count);
   }

   count++;

   if(root.right != null){
       NumbOfNodes(root.right, count);
   }

   return count;
}

【讨论】:

    【解决方案3】:

    由于 NumOfNodes 本身是一个变量,它是一个实例,您可以使用 this.count 将自身缓存为一个值

    var NumbOfNodes = function(root){
      if (this.count === undefined) {
        this.count = 0;
      }
    
       if(root.left != null){
           NumbOfNodes(root.left);
       }
    
       this.count++;
    
       if(root.right != null){
           NumbOfNodes(root.right);
       }
    
       return this.count;
    }
    

    【讨论】:

      【解决方案4】:

      你真的需要一个额外的变量来维持计数吗?

      试试这个,

      var NumbOfNodes = function(root){
         if(!root){
          return 0;
         }   
         return NumbOfNodes(root.right) + NumbOfNodes(root.left) + 1; 
      }
      

      【讨论】:

        【解决方案5】:

        为了保留变量的值,您需要递归地传递它。比如:

        var NumbOfNodes = numberOfNodes(roots, 0)
        
        function numberOfNodes(root, count){
        
            if(root.left != null){
                numberOfNodes(root.left, count);
            }
        
            count++;
        
            if(root.right != null){
                numberOfNodes(root.right, count);
            }
        
            return count;
         }
        

        【讨论】:

          【解决方案6】:

          如何在递归函数 (Javascript) 中保存要递增的值

          您不需要保存值来计算节点数。设计递归函数时,只需考虑 baseinductive 情况

          • 基本情况 - node 为空,返回计数 0
          • 归纳案例 - node 为空,此节点返回 1,此节点的 leftright 节点的计数。

          我们的countNodes 函数几乎是自己写的

          const Empty =
            Symbol ()
          
          const Node = (value = null, left = Empty, right = Empty) =>
            ({ Node, value, left, right })
            
          const countNodes = (node = Empty) =>
            node === Empty
              ? 0
              : 1 + countNodes (node.left) + countNodes (node.right)
              
          console.log
            ( countNodes ()                                       // 0
          
            , countNodes (Node (0))                               // 1
          
            , countNodes (Node (0, Node (1), Node (2)))           // 3
          
            , countNodes (Node (0, Node (1, Node (2), Node (3))
                                 , Node (4, Node (5), Node (6)))) // 7
            )

          【讨论】:

            猜你喜欢
            • 2021-03-03
            • 1970-01-01
            • 2012-07-18
            • 2016-02-04
            • 1970-01-01
            • 1970-01-01
            • 2018-10-11
            • 1970-01-01
            • 2019-10-14
            相关资源
            最近更新 更多