【问题标题】:Understanding how to recursively inorder traverse a binary tree了解如何递归中序遍历二叉树
【发布时间】:2022-11-21 17:32:45
【问题描述】:

任何关于我将如何去做的指导都会很棒。我完全迷路了。有单独的方法和类来获取根等等。我感到困惑的是如何处理给定的 upperBound、lowerBound 和消费者,因为我查找的所有内容都只有一个根参数

     A recursive implementation of a bounded inorder traversal.
     @param lowerBound  The lower bound of the bounded inorder traversal.
     @param upperBound  The upper bound of the bounded inorder traversal.
     @param consumer  The consumer to call to process each entry in the traversal.
    */
   public void boundedInorderTraverse(T lowerBound, T upperBound, Consumer<T> consumer) {
      // TODO(student) Implement this method.
   }

【问题讨论】:

    标签: java recursion tree binary-search-tree bounds


    【解决方案1】:

    简洁的“按顺序”遍历取决于图形的结构。 假设您的定义与维基百科的定义相匹配,您必须编写的算法将是

    https://en.wikipedia.org/wiki/Tree_traversal#In-order,_LNR

    1. 递归遍历当前节点的左子树。
    2. 访问当前节点(图中:绿色位置)。
    3. 递归遍历当前节点的右子树。

      更简洁的答案需要我们更多地了解您的图形和 T。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-01
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 2020-09-25
      • 2010-11-20
      相关资源
      最近更新 更多