简体   繁体   English

空 output 同时在 Java 中的二叉树中执行级别顺序遍历

[英]Empty output while performing level order traversal in Binary trees in Java

I wanted to perform level order traversal in Binary Trees in Java.我想在 Java 的二叉树中执行级别顺序遍历。 Basically I needed to store values of nodes of each level in an array list and store such different array lists in one single array list.基本上我需要将每个级别的节点值存储在一个数组列表中,并将这些不同的数组列表存储在一个数组列表中。 In my final output, only empty array lists are being shown.在我的最终 output 中,只显示了空数组列表。 I can not understand the logical error.我无法理解逻辑错误。 Can anyone please guide me.任何人都可以请指导我。

public class Solution {
        static ArrayList<ArrayList<Integer>> ourList=new ArrayList<>();
        static ArrayList<Integer>ourArray=new ArrayList<>();
        public static void breadth(Node root){
            Queue<Node> ourQueue=new LinkedList<>();
            ourQueue.add(root);
            while(!ourQueue.isEmpty()){
                int size=ourQueue.size();
                for(int i=0;i<size;i++){
                    Node poppedElement=ourQueue.poll();
                    ourArray.add(poppedElement.data);
                    if(poppedElement.left!=null){
                        ourQueue.add(poppedElement.left);
                    }
                    if(poppedElement.right!=null){
                        ourQueue.add(poppedElement.right);
                    }
                }
                ourList.add(ourArray);
                ourArray.clear();
            }
        }
    }

As you want to collect the results in ourList , you need to create dedicated instances of the lists kept in ourArray .当您想在ourList中收集结果时,您需要创建保存在ourArray中的列表的专用实例。 So, instead of clearing ourArray , you should probably re-create it: Instead of因此,您应该重新创建它,而不是清除ourArray :而不是

ourArray.clear();

write

ourArray= new ArrayList<>();

Do the following short test:做以下简短的测试:

    ArrayList<ArrayList<Integer>> ourList=new ArrayList<>();
    ArrayList<Integer>ourArray=new ArrayList<>();
    //add values
    ourArray.add(1);  ourArray.add(2);  ourArray.add(3);  ourArray.add(4);
    ourList.add(ourArray);

    System.out.println(ourList.get(0)); //prints [1, 2, 3, 4]
    ourArray.clear();
    System.out.println(ourList.get(0)); //prints []

The reason is that ourList contains a reference to a list.原因是ourList包含对列表的引用。 ourArray is a reference to the same list, and when it is cleared, ourList contains a reference to an empty list. ourArray是对同一个列表的引用,当它被清除时, ourList包含对空列表的引用。

Now test the solution proposed by PHolzwarth :现在测试PHolzwarth提出的解决方案:

    System.out.println(ourList.get(0)); //prints [1, 2, 3, 4]
    ourArray = new ArrayList<>();
    System.out.println(ourList.get(0)); //prints [1, 2, 3, 4]

An alternative solution is a "defensive copy":另一种解决方案是“防御性副本”:

    ourList.add(new ArrayList<>(ourArray)); //add a copy of ourArray

    System.out.println(ourList.get(0)); //prints [1, 2, 3, 4]
    ourArray.clear();
    System.out.println(ourList.get(0)); //prints [1, 2, 3, 4]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM