编辑代码

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    public static class Node {
        int id;
        ArrayList<Node> children = new ArrayList<>();
        int level = 0;

        public Node(int id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return String.format("id:%s,level:%s", this.id, this.level);
        }
    }

    public static void main(String[] args) {
        Node a = new Node(1);
        Node b = new Node(2);
        Node c = new Node(3);
        Node d = new Node(4);
        Node e = new Node(5);
        Node f = new Node(6);
        a.children.add(b);
        a.children.add(c);
        b.children.add(d);
        b.children.add(e);
        b.children.add(f);
        measureLevel(a);
    }

    public static void measureLevel(Node node) {
        //TODO write your impl here
        Queue<Node> myQueue = new LinkedList<>();
        myQueue.add(node);
        while (!myQueue.isEmpty()){
            Node myNode = myQueue.poll();
            System.out.println(myNode.toString());
            if(myNode.children != null && myNode.children.size() > 0){
                for (Node child : myNode.children) {
                    child.level = myNode.level+1;
                    myQueue.add(child);
                }
            }
        }
    }
}