【问题标题】:Level Order Traversal w/ Binary Trees using Java and Recursion使用 Java 和递归的带二叉树的级别顺序遍历
【发布时间】:2015-05-10 07:52:01
【问题描述】:

在使用递归时,我的二叉树的级别顺序遍历存在问题。我正在输入以下值:50,60,70,30,20,10 这是我正在使用的代码:

    public void levelOrder(Node localRoot){
    if(localRoot != null){
        if(localRoot.leftChild != null && localRoot.rightChild != null){
            System.out.print(localRoot.iData + " ");
            System.out.print(localRoot.leftChild.iData + " ");
            System.out.print(localRoot.rightChild.iData + " ");
            levelOrder(localRoot.leftChild);
            levelOrder(localRoot.rightChild);
        }
        else if(localRoot.rightChild == null && localRoot.leftChild == null){
            ;
        }
        else if(localRoot.rightChild == null){
            System.out.print(localRoot.leftChild.iData + " ");
            //levelOrder(localRoot.leftChild);
        }
        else{
            System.out.print(localRoot.rightChild.iData + " ");
            //levelOrder(localRoot.rightChild);
        }
    }
}

不使用堆栈是否可以递归?因为目前这个功能把我一直带到左边然后它向右走。我可以做些什么不同的事情?

我对这段代码的输出是:50、30、60、20、70,它不打印 10。

【问题讨论】:

  • 与递归的处理是您不必自己管理堆栈;)。
  • 我的意思是.. 任何递归方法都可以用堆栈替换,我知道我不必自己管理堆栈,但在触摸右侧之前我会一直沿着完整的左侧向下移动。
  • 栈是指队列吗?如果我没记错的话,级别顺序与广度优先相同,它需要一个队列并且不是递归的。
  • 看起来你在做预购遍历。 @thermite 我同意,请参阅 this link 以了解有关队列执行级别顺序的讨论。
  • 因为左边的东西应该在节点之前,而右边的东西应该在进程正确运行之后出现。我建议您研究一下构建树的机制。

标签: java recursion binary-tree


【解决方案1】:

有趣的是,这是一个相当常见的问题(谷歌“recursive bread first traversal”,并且有几个类似答案的 stackoverflow 链接)

目前最好的就在这里

Performing Breadth First Search recursively

我同意最佳答案的作者,将迭代算法(广度优先遍历)转换为递归解决方案确实没有意义。如前所述,是的,很容易将迭代转变为尾递归,但目的是什么?你仍然需要一个队列。

【讨论】:

    【解决方案2】:

    公共无效级别() {

     printLevel(root);
    

    }

    public void printLevel(Node current)
    {
        if(current != null){
            if(current.leftChild != null && current.rightChild != null){
                System.out.print(current.iData + " ");
                System.out.print(current.leftChild.iData + " ");
                System.out.print(current.rightChild.iData + " ");
                printLevel(current.leftChild);
                printLevel(current.rightChild);
            }
            else if(current.rightChild == null && current.leftChild == null){
            ;
            }
            else if(current.rightChild == null){
                System.out.print(current.leftChild.iData + " ");
                //levelOrder(current.leftChild);
            }
            else{
                System.out.print(current.rightChild.iData + " ");
                //levelOrder(current.rightChild);
            }
        }
    }   
    

    【讨论】:

      【解决方案3】:

      您可以使用队列来解决问题:

      private Queue<Key> getLevelOrderKeys(Node root){
      
          if (root == null) {
              return null;
          }
      
          Queue<Key> keyQueue = new ArrayDeque<Key>(); //return value. Queue with the level order keys
          Queue<Node> nodeQueue = new ArrayDeque<Node>();
      
          nodeQueue.add(root);
      
          //while there is at least one discovered node
          while(!nodeQueue.isEmpty()) {
      
              Node current = nodeQueue.poll();
              keyQueue.add(current.key);
      
              if (current.left != null){
                  nodeQueue.add(current.left);
              }
      
              if (current.right != null){
                  nodeQueue.add(current.right);
              }
      
          }
      
          return keyQueue;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-01
        • 1970-01-01
        • 2016-05-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-28
        • 1970-01-01
        相关资源
        最近更新 更多