<bdo id='2y0Gs'></bdo><ul id='2y0Gs'></ul>

  • <legend id='2y0Gs'><style id='2y0Gs'><dir id='2y0Gs'><q id='2y0Gs'></q></dir></style></legend>
    <tfoot id='2y0Gs'></tfoot>

    1. <small id='2y0Gs'></small><noframes id='2y0Gs'>

        <i id='2y0Gs'><tr id='2y0Gs'><dt id='2y0Gs'><q id='2y0Gs'><span id='2y0Gs'><b id='2y0Gs'><form id='2y0Gs'><ins id='2y0Gs'></ins><ul id='2y0Gs'></ul><sub id='2y0Gs'></sub></form><legend id='2y0Gs'></legend><bdo id='2y0Gs'><pre id='2y0Gs'><center id='2y0Gs'></center></pre></bdo></b><th id='2y0Gs'></th></span></q></dt></tr></i><div id='2y0Gs'><tfoot id='2y0Gs'></tfoot><dl id='2y0Gs'><fieldset id='2y0Gs'></fieldset></dl></div>

        有返回问题的总和的所有路径

        All Paths for a Sum with return issues(有返回问题的总和的所有路径)

        <tfoot id='0D7K8'></tfoot>
      1. <small id='0D7K8'></small><noframes id='0D7K8'>

        <legend id='0D7K8'><style id='0D7K8'><dir id='0D7K8'><q id='0D7K8'></q></dir></style></legend>

                <bdo id='0D7K8'></bdo><ul id='0D7K8'></ul>
                <i id='0D7K8'><tr id='0D7K8'><dt id='0D7K8'><q id='0D7K8'><span id='0D7K8'><b id='0D7K8'><form id='0D7K8'><ins id='0D7K8'></ins><ul id='0D7K8'></ul><sub id='0D7K8'></sub></form><legend id='0D7K8'></legend><bdo id='0D7K8'><pre id='0D7K8'><center id='0D7K8'></center></pre></bdo></b><th id='0D7K8'></th></span></q></dt></tr></i><div id='0D7K8'><tfoot id='0D7K8'></tfoot><dl id='0D7K8'><fieldset id='0D7K8'></fieldset></dl></div>

                  <tbody id='0D7K8'></tbody>

                  本文介绍了有返回问题的总和的所有路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个问题是要找到总和的所有路径.问题是:

                  <块引用>

                  给定一棵二叉树和一个数字S",找到从根到叶的所有路径,使得每条路径的所有节点值之和等于S".

                  我的递归方法是:

                  def all_sum_path(root, target):结果 = []find_sum_path(根,目标,结果,[])返回结果def find_sum_path(根,目标,结果,新路径):如果不是根:返回无new_path.append(root.value)diff = 目标 - root.value如果不是 root.left 而不是 root.right 并且 diff == 0:# 复制列表的值而不是引用结果.附加(列表(新路径))如果root.left:return find_sum_path(root.left, diff, result, new_path)如果root.right:return find_sum_path(root.right, diff, result, new_path)删除新路径[-1]类树节点():def __init__(self, _value):self.value = _valueself.left,self.right,self.next = 无,无,无定义主():根 = 树节点(1)root.left = 树节点(7)root.right = TreeNode(9)root.left.left = TreeNode(4)root.left.right = TreeNode(5)root.right.left = TreeNode(2)root.right.right = TreeNode(7)打印(all_sum_path(根,12))根 = 树节点(12)root.left = 树节点(7)root.right = 树节点(1)root.left.left = TreeNode(4)root.right.left = TreeNode(10)root.right.right = TreeNode(5)打印(all_sum_path(根,23))主要的()

                  输出是:

                  [[1, 7, 4]][[12, 7, 4]]进程以退出代码 0 结束

                  但是,正确的做法应该是:

                  def all_sum_path(root, target):结果 = []find_sum_path(根,目标,结果,[])返回结果def find_sum_path(根,目标,结果,新路径):如果不是根:返回无new_path.append(root.value)diff = 目标 - root.value如果不是 root.left 而不是 root.right 并且 diff == 0:# 复制列表的值而不是引用结果.附加(列表(新路径))如果root.left:find_sum_path(root.left, diff, result, new_path)如果root.right:find_sum_path(root.right, diff, result, new_path)删除新路径[-1]类树节点():def __init__(self, _value):self.value = _valueself.left,self.right,self.next = 无,无,无定义主():根 = 树节点(1)root.left = 树节点(7)root.right = TreeNode(9)root.left.left = TreeNode(4)root.left.right = TreeNode(5)root.right.left = TreeNode(2)root.right.right = TreeNode(7)打印(all_sum_path(根,12))根 = 树节点(12)root.left = 树节点(7)root.right = 树节点(1)root.left.left = TreeNode(4)root.right.left = TreeNode(10)root.right.right = TreeNode(5)打印(all_sum_path(根,23))主要的()

                  有输出:

                  [[1, 7, 4], [1, 9, 2]][[12, 7, 4], [12, 1, 10]]进程以退出代码 0 结束

                  我在这里有一些问题:

                  1. 为什么我们在递归语句中不需要return?我也对 return 语句如何将输出减少到只有一个感兴趣?

                  2. 为什么我们不需要 result = find_sum_path(root, target, result, [])?那么更新结果背后的逻辑是什么?

                  3. 不知道为什么时间复杂度是O(N^2)?

                  <块引用>

                  上述算法的时间复杂度为O(N^2),其中‘N’是树中节点的总数.这是因为我们遍历每个节点一次(这将花费 O(N)),并且对于每个叶节点,我们可能必须存储它的路径,这将花费 O(N).

                  提前感谢您的帮助.

                  解决方案

                  首先,我想说你已经非常接近解决问题了,你已经完成了一项了不起的工作.递归是一种函数式遗产,因此将其与函数式风格一起使用会产生最佳结果.这意味着要避免诸如突变、变量重新分配和其他副作用之类的事情.这可以消除许多错误来源(和令人头疼的问题)!

                  为了美化你的程序,我们可以先修改 TreeNode 以在构造时同时接受 leftright 参数 -

                  类 TreeNode():def __init__(self, value, left=None, right=None):自我价值=价值self.left = 左self.right = 对

                  现在我们可以定义两棵树,t1t2.请注意,我们不会重新分配 root -

                  def main():t1 = 树节点 ( 1, 树节点 (7, 树节点 (4), 树节点 (5)), 树节点 (9, 树节点 (2), 树节点 (7)))t2 = 树节点 ( 12,树节点(7,树节点(4),无), 树节点 (1, 树节点 (10), 树节点 (5)))打印(all_sum_path(t1,12))打印(all_sum_path(t2,23))主要的()

                  预期的输出是 -

                  [[1, 7, 4], [1, 9, 2]][[12, 7, 4], [12, 1, 10]]

                  最后我们实现了find_sum.我们可以使用数学归纳法为我们的函数写一个简单的案例分析-

                  1. 如果输入树t为空,则返回空结果
                  2. (inductive) tnot 空的.如果 t.value 与目标 q 匹配,则已找到解决方案;将 t.value 添加到当前 path 和 yield
                  3. (inductive) t 不为空,并且 t.value 与目标 q 匹配 not.将 t.value 添加到当前 path 和新的子问题 next_q;解决 t.leftt.right 分支上的子问题 -

                  def find_sum (t, q, path = []):如果不是 t:返回 # (1)elif t.value == q:yield [*path, t.value] # (2)别的:next_q = q - t.value # (3)next_path = [*路径,t.value]从 find_sum(t.left, next_q, next_path) 中产生来自 find_sum(t.right, next_q, next_path)

                  请注意我们如何不使用上面的 .append 之类的突变.为了计算 all 路径,我们可以将 all_find_sum 写成 find_sum -

                  的展开

                  def all_sum_path (t, q):返回列表(find_sum(t,q))

                  就是这样,你的程序已经完成了:D


                  如果不想使用单独的生成器find_sum,我们可以就地扩展生成器-

                  def all_sum_path (t, q, path = []):如果不是 t:返回 []elif t.value == q:返回 [[*path, t.value]]别的:返回 [ *all_sum_path(t.left, q - t.value, [*path, t.value]), *all_sum_path(t.right, q - t.value, [*path, t.value])]

                  请注意这两种变体之间的明显相似之处.任何编写良好的程序都可以轻松地在两种风格之间转换.

                  I have a question in finding the all paths for a sum. The question is:

                  Given a binary tree and a number ‘S’, find all paths from root-to-leaf such that the sum of all the node values of each path equals ‘S’.

                  My approach with recursion is:

                  def all_sum_path(root, target):
                      result = []
                      find_sum_path(root, target, result, [])
                      return result
                  
                  def find_sum_path(root, target, result, new_path):
                      if not root:
                          return None
                      new_path.append(root.value)
                      diff = target - root.value
                      if not root.left and not root.right and diff == 0:
                          # copy the value of the list rather than a reference
                          result.append(list(new_path))
                      if root.left:
                          return find_sum_path(root.left, diff, result, new_path)
                      if root.right:
                          return find_sum_path(root.right, diff, result, new_path)
                      del new_path[-1]
                  
                  
                  class TreeNode():
                      def __init__(self, _value):
                          self.value = _value
                          self.left, self.right, self.next = None, None, None
                  
                  def main():
                      root = TreeNode(1)
                      root.left = TreeNode(7)
                      root.right = TreeNode(9)
                      root.left.left = TreeNode(4)
                      root.left.right = TreeNode(5)
                      root.right.left = TreeNode(2)
                      root.right.right = TreeNode(7)
                  
                      print(all_sum_path(root, 12))
                  
                      root = TreeNode(12)
                      root.left = TreeNode(7)
                      root.right = TreeNode(1)
                      root.left.left = TreeNode(4)
                      root.right.left = TreeNode(10)
                      root.right.right = TreeNode(5)
                  
                      print(all_sum_path(root, 23))
                  
                  main()
                  

                  and the output is:

                  [[1, 7, 4]]
                  [[12, 7, 4]]
                  
                  Process finished with exit code 0
                  

                  However, the correct approach should be:

                  def all_sum_path(root, target):
                      result = []
                      find_sum_path(root, target, result, [])
                      return result
                  
                  def find_sum_path(root, target, result, new_path):
                      if not root:
                          return None
                      new_path.append(root.value)
                      diff = target - root.value
                      if not root.left and not root.right and diff == 0:
                          # copy the value of the list rather than a reference
                          result.append(list(new_path))
                      if root.left:
                          find_sum_path(root.left, diff, result, new_path)
                      if root.right:
                          find_sum_path(root.right, diff, result, new_path)
                      del new_path[-1]
                  
                  
                  class TreeNode():
                      def __init__(self, _value):
                          self.value = _value
                          self.left, self.right, self.next = None, None, None
                  
                  def main():
                      root = TreeNode(1)
                      root.left = TreeNode(7)
                      root.right = TreeNode(9)
                      root.left.left = TreeNode(4)
                      root.left.right = TreeNode(5)
                      root.right.left = TreeNode(2)
                      root.right.right = TreeNode(7)
                  
                      print(all_sum_path(root, 12))
                  
                      root = TreeNode(12)
                      root.left = TreeNode(7)
                      root.right = TreeNode(1)
                      root.left.left = TreeNode(4)
                      root.right.left = TreeNode(10)
                      root.right.right = TreeNode(5)
                  
                      print(all_sum_path(root, 23))
                  
                  main()
                  

                  With output:

                  [[1, 7, 4], [1, 9, 2]]
                  [[12, 7, 4], [12, 1, 10]]
                  
                  Process finished with exit code 0
                  

                  I have a some questions here:

                  1. Why don't we need a return in the recursion statement? I am also interested in how the return statement reduced the output to only one?

                  2. Why don't we need the result = find_sum_path(root, target, result, [])? Then what is the logic behind to update the results?

                  3. I am not sure why the time complexity is O(N^2)?

                  The time complexity of the above algorithm is O(N^2), where ‘N’ is the total number of nodes in the tree. This is due to the fact that we traverse each node once (which will take O(N)), and for every leaf node we might have to store its path which will take O(N).

                  Thanks for your help in advance.

                  解决方案

                  First off, I want to say you're very close to solving the problem and you've done a terrific job. Recursion is a functional heritage and so using it with functional style yields the best results. This means avoiding things like mutations, variable reassignments, and other side effects. This can eliminate many sources of bugs (and headaches)!

                  To spruce up your program, we can start by modifying TreeNode to accept both left and right parameters at time of construction -

                  class TreeNode():
                    def __init__(self, value, left=None, right=None):
                      self.value = value
                      self.left = left
                      self.right = right
                  

                  Now we can define two trees, t1 and t2. Notice we don't reassign root -

                  def main():
                    t1 = TreeNode 
                      ( 1
                      , TreeNode(7, TreeNode(4), TreeNode(5))
                      , TreeNode(9, TreeNode(2), TreeNode(7))
                      )
                      
                    t2 = TreeNode 
                      ( 12
                      , TreeNode(7, TreeNode(4), None)
                      , TreeNode(1, TreeNode(10), TreeNode(5))
                      )
                  
                    print(all_sum_path(t1, 12))
                    print(all_sum_path(t2, 23))
                  
                  main()
                  

                  The expected output is -

                  [[1, 7, 4], [1, 9, 2]]
                  [[12, 7, 4], [12, 1, 10]]
                  

                  Finally we implement find_sum. We can use mathematical induction to write a simple case analysis for our function -

                  1. if the input tree t is empty, return the empty result
                  2. (inductive) t is not empty. if t.value matches the target, q, a solution has been found; add t.value to the current path and yield
                  3. (inductive) t is not empty and t.value does not match the target q. add t.value to the current path and new sub-problem, next_q; solve the sub-problem on t.left and t.right branches -

                  def find_sum (t, q, path = []):
                    if not t:
                      return                        # (1)
                    elif t.value == q:
                      yield [*path, t.value]        # (2)
                    else:
                      next_q = q - t.value          # (3)
                      next_path = [*path, t.value]
                      yield from find_sum(t.left, next_q, next_path)
                      yield from find_sum(t.right, next_q, next_path)
                  

                  Notice how we don't use mutations like .append above. To compute all paths, we can write all_find_sum as the expansion of find_sum -

                  def all_sum_path (t, q):
                    return list(find_sum(t, q))
                  

                  And that's it, your program is done :D


                  If you don't want to use a separate generator find_sum, we can expand the generator in place -

                  def all_sum_path (t, q, path = []):
                    if not t:
                      return []
                    elif t.value == q:
                      return [[*path, t.value]]
                    else:
                      return 
                        [ *all_sum_path(t.left, q - t.value, [*path, t.value])
                        , *all_sum_path(t.right, q - t.value, [*path, t.value])
                        ]
                  

                  Notice the distinct similarity between the two variations. Any well-written program can easily be converted between either style.

                  这篇关于有返回问题的总和的所有路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                  • <i id='vEyrQ'><tr id='vEyrQ'><dt id='vEyrQ'><q id='vEyrQ'><span id='vEyrQ'><b id='vEyrQ'><form id='vEyrQ'><ins id='vEyrQ'></ins><ul id='vEyrQ'></ul><sub id='vEyrQ'></sub></form><legend id='vEyrQ'></legend><bdo id='vEyrQ'><pre id='vEyrQ'><center id='vEyrQ'></center></pre></bdo></b><th id='vEyrQ'></th></span></q></dt></tr></i><div id='vEyrQ'><tfoot id='vEyrQ'></tfoot><dl id='vEyrQ'><fieldset id='vEyrQ'></fieldset></dl></div>
                    <tfoot id='vEyrQ'></tfoot>
                      <tbody id='vEyrQ'></tbody>
                        <bdo id='vEyrQ'></bdo><ul id='vEyrQ'></ul>

                            <legend id='vEyrQ'><style id='vEyrQ'><dir id='vEyrQ'><q id='vEyrQ'></q></dir></style></legend>

                            <small id='vEyrQ'></small><noframes id='vEyrQ'>