【问题标题】:Binary tree, print only one level (BFS)二叉树,只打印一层(BFS)
【发布时间】:2013-10-18 05:46:31
【问题描述】:

我想知道如何只打印特定级别的二叉树。我在这里阅读了很多关于 BFS 的问题,但没有发现任何关于 printin 的问题。

我应该如何将常见的 BFS 搜索更改为仅打印此树的第 2 级:

   6
  / \
 4   8
/ \ / \
1 5 7  9

第 2 级将是 1、5、7、9。谢谢!

【问题讨论】:

  • 追踪你目前的等级;并在达到 2 时终止。
  • 这很明显,但我找不到使用常规 BFS 的方法。它使用节点处理队列,但不区分级别。它只是按 BFS 顺序排列和处理它们。我得到的只是一些奇怪的解决方案,检查每个级别应该有的节点数量,但是当我想到一个非完整的树时它会崩溃
  • 一种方法是将 {level,node} 而不仅仅是 {node} 的元组加入队列。

标签: tree traversal


【解决方案1】:

你需要在你的节点上有一个 level 属性。然后当你在树上遍历时,你会问:

if (level == 2) //or whatever level you wish
{
    ...
}

这是一个很好的例子:Find all nodes in a binary tree on a specific level (Interview Query)

如果节点上没有级别并且您无法更改它,那么您可以在您进行检查的方法中将其作为全局变量 - 不是可取的,而是另一种解决方案。我没有在代码中检查这个答案,但我相信它应该非常接近解决方案。

例如:

int level = 0;

     public void PrintOneLevelBFS(int levelToPrint)    
     {      
        Queue q = new Queue();
        q.Enqueue(root); //Get the root of the tree to the queue.

        while (q.count > 0)
        {
            level++; //Each iteration goes deeper - maybe the wrong place to add it (but somewhere where the BFS goes left or right - then add one level).
            Node node = q.DeQueue();

            if (level == levelToPrint)
            {
                ConsoleWriteLine(node.Value) //Only write the value when you dequeue it
            }

            if (node.left !=null)
            {
                q.EnQueue(node.left); //enqueue the left child
            }
            if (n.right !=null)
            {
                q.EnQueue(node.right); //enqueue the right child
            }
         }
     }

【讨论】:

  • 谢谢 Misha,这是一个很好的解决方案,但是如果您不能在节点上拥有该属性怎么办?我在计算机科学大学,我认为这可能是一个测试练习。
  • 嗯,我不认为它会起作用。每次循环运行时它都会增加级别(1个循环= 1个节点,然后,不是每个级别)。
  • 是的,这就是为什么你应该考虑把关卡放在正确的位置,而且,如果你保持相同的关卡而不是你不应该添加它,也许你可以在做关卡之前再检查一下++
【解决方案2】:

好的,我从教授那里得到了类似问题的答案。

在一棵二叉搜索树中,找到某一层的最小数 (GenericTree 和 GenericQueue 是该课程的特定课程。我还翻译了整个练习,所以有些事情可能听起来很奇怪:P

public int calculateMinimum( BinaryTree<Integer> tree, int n ){
    GenericQueue<BinaryTree<Integer>> queue = new GenericQueue<BinaryTree<Integer>>();
    queue.push(tree);
    queue.push(new BinaryTree<Integer>(-1));
    int nActual = 0; //actual level
    while (!queue.isEmpty()){
        tree = queue.pop();
        if (nActual == n){
            int min = tree.getRootData();
            while (!queue.isEmpty()){
                tree = queue.pop();
                if (!tree.getRootData().equals(-1) && (tree.getRootData()<min))
                    min = tre.getRootData();
            }
            return min;
        }
        if (!tree.getLeftChild().getRootData() == null))
            queue.push(tree.getLeftChild());
        if (!tree.getRightChild().getRootData() == null))
            queue.push(tree.getRightChild());
        if ((tree.getRootData().equals(-1) && (!queue.isEmpty())){
            nActual++;
            queue.push(new BinaryTree<Integer>(-1));
        }
    }
    return -1;
}                

【讨论】:

    猜你喜欢
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    相关资源
    最近更新 更多