今日头条面试题-多叉树的层级遍历(返回二维链表)

面试题:有一棵多叉树,要求按照层级遍历,方法返回值是二维链表,每个链表是该层元素的所有值的链表。

如:
今日头条面试题-多叉树的层级遍历(返回二维链表)_第1张图片
程序运行的结果为:
[
1
2–>3–>4
5–>6–>7>–8-->9–>10
11–>12–>13
]

解题思路:
对于树的层级遍历,首先想到的是队列。如果本题直接层级遍历多叉树,可以参考二叉树的层级遍历,先把根节点放入队列,然后从队列头取出一个节点,然后把这个节点子节点放入队列尾,直到队列中没有元素的时候,遍历结束,这样返回的是一条链表。
但是现在需要返回二维链表,也可以用这种方式遍历,关键是什么时候来创建一个新链表呢?

首先,可以使用两个队列parent,children。parent保存当前层的节点,children队列都是parent的子节点。
①先将根节点放入parent队列:
访问root节点放入parent对象
②然后循环弹出parent的头结点node,如果不为空,则说明该层的节点还没有访问结束,则把该节点的子节点依次放入到children列表尾。
children
如果node节点为空,说明该层的节点访问结束,children队列中所有的节点就是下一层的所有节点,此时需要新建一条链表。
然后将parent队列的元素更换为children中的元素,清空children队列。
循环第②步。当parent队列和children队列都为空时,访问结束。

java 代码:

/**
 * 多叉树节点
 */
public class MultiWayTree {
    int value;
    List children;

    public List traverse(MultiWayTree root){
    	if(root == null){
            return new ArrayList<>();
        }
        LinkedList parent = new LinkedList<>();
        LinkedList children = new LinkedList<>();

        List result = new ArrayList<>();
        ListNode listNode = new ListNode(-1, null);

        parent.add(root);
        result.add(new ListNode(root.value, null));
        while (!parent.isEmpty() || !children.isEmpty()){
            if (! parent.isEmpty()){ // parent队列不为空时, 将头节点的子节点放入children队列.
                MultiWayTree node = parent.pop();
                node.children.forEach(child -> children.add(child));
            }else{// parent队列为空时, 构造一个链表,将children 中的节点依次插入到链表尾.
                for(MultiWayTree child : children){
                    insertTail(listNode, new ListNode(child.value, null));
                }

                result.add(listNode.next);
                listNode = new ListNode(-1, null);
                // 将parent队列替换为children 队列
                parent.addAll(children);
                // 清空children队列
                children.clear();
            }
        }
        return result;
    }

    private ListNode insertTail(ListNode head, ListNode newNode){
        ListNode node = head;
        while (node.next !=null){
            node = node.next;
        }
        node.next = newNode;
        return head;
    }

    public MultiWayTree(int value, List children) {
        this.value = value;
        this.children = children;
    }
    public MultiWayTree(int value) {
        this.value = value;
        this.children = new ArrayList<>();
    }

    @Override
    public String toString() {
        return "MultiWayTree{" +
                "value=" + value +
                ", children=" + children +
                '}';
    }

    public MultiWayTree() {}

    /**
     * 链表节点定义
     */
    class ListNode{
        int value;
        ListNode next;

        public ListNode(int value, ListNode next) {
            this.value = value;
            this.next = next;
        }
    }

}

测试类:

public class MultiWayTreeTest {
    public static void main(String[] args) {
        MultiWayTree node1 = new MultiWayTree(1);
        MultiWayTree node2 = new MultiWayTree(2);
        MultiWayTree node3 = new MultiWayTree(3);
        MultiWayTree node4 = new MultiWayTree(4);
        MultiWayTree node5 = new MultiWayTree(5);
        MultiWayTree node6 = new MultiWayTree(6);
        MultiWayTree node7 = new MultiWayTree(7);
        MultiWayTree node8 = new MultiWayTree(8);
        MultiWayTree node9 = new MultiWayTree(9);
        MultiWayTree node10 = new MultiWayTree(10);
        MultiWayTree node11 = new MultiWayTree(11);
        MultiWayTree node12 = new MultiWayTree(12);
        MultiWayTree node13 = new MultiWayTree(13);

        List children = new ArrayList<>();
        children.add(node11);
        children.add(node12);
        node8.children = children;

        children = new ArrayList<>();
        children.add(node13);
        node10.children = children;

        children = new ArrayList<>();
        children.add(node7);
        children.add(node8);
        children.add(node9);
        children.add(node10);
        node4.children = children;

        children = new ArrayList<>();
        children.add(node5);
        children.add(node6);
        node2.children = children;

        children = new ArrayList<>();
        children.add(node2);
        children.add(node3);
        children.add(node4);
        node1.children = children;

        List result = node1.traverse(node1);
        for(int i = 0 ;i < result.size(); i ++){
            MultiWayTree.ListNode node = result.get(i);
            System.out.println("第" + i +"层节点:");
            while (node !=null){
                System.out.print(node.value + " ");
                node = node.next;
            }
            System.out.println("\n- - - - - - - - - - - - - - - - - ");
        }

    }
}

运行结果:

第0层节点:
1 
- - - - - - - - - - - - - - - - - 
第1层节点:
2 3 4 
- - - - - - - - - - - - - - - - - 
第2层节点:
5 6 7 8 9 10 
- - - - - - - - - - - - - - - - - 
第3层节点:
11 12 13 
- - - - - - - - - - - - - - - - -

你可能感兴趣的:(面试题,树,多叉树,数据结构,java,面试题)