• ⭐北邮复试刷题429. N 叉树的层序遍历(按层入队出队BFS)(力扣每日一题)


    429. N 叉树的层序遍历

    给定一个 N 叉树,返回其节点值的层序遍历。(即从左到右,逐层遍历)。

    树的序列化输入是用层序遍历,每组子节点都由 null 值分隔(参见示例)。

    示例 1:
    
    
    
    输入:root = [1,null,3,2,4,null,5,6]
    输出:[[1],[3,2,4],[5,6]]
    
    示例 2:
    
    
    
    输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
    输出:[[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]
     
    
    提示:
    树的高度不会超过 1000
    树的节点总数在 [0, 10^4] 之间
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    题解:

    主要思路就是常规BFS,只不过每次从队列拿元素的时候一次将一层的节点全部拿出,再将这些节点下层的children都同时入队即可。这样会造成最后多一次入队出队,所以最后加上一次判空操作即可;

    代码:

    /*
    // Definition for a Node.
    class Node {
        public int val;
        public List children;
    
        public Node() {}
    
        public Node(int _val) {
            val = _val;
        }
    
        public Node(int _val, List _children) {
            val = _val;
            children = _children;
        }
    };
    */
    
    class Solution {
        public List<List<Integer>> levelOrder(Node root) {
            Queue<Node> queue = new LinkedList<>();
            List<List<Integer>> res = new ArrayList<>();
            if(root == null){
                return res;
            }
    
            queue.offer(root);
            List<Integer> list = new ArrayList<>();
            list.add(root.val);
            res.add(list);
            while(!queue.isEmpty()){
                // 队列想边拿边加时,则利用长度字段;想只拿不加可以用队列判空,最后队列再加;
                List<List<Node>> tmpList = new ArrayList<>();
                while(!queue.isEmpty()){
                    // 出队
                    // 将同层的节点一次拿出
                    Node tmpQueueOne = queue.poll();
                    if(tmpQueueOne.children != null)
                        tmpList.add(tmpQueueOne.children);
                }
                if(tmpList == null || tmpList.size() == 0)
                    continue;
                int tmpLen = tmpList.size();
                List<Integer> tmpResOfOne = new ArrayList<>();
                for(int i=0;i<tmpLen;i++){
                    if(tmpList.get(i) != null){
                        for(int j=0;j<tmpList.get(i).size();j++){
                            tmpResOfOne.add(tmpList.get(i).get(j).val);
                            // 入队
                            // 将本层下层的孩子全部同时入队
                            queue.offer(tmpList.get(i).get(j));
                        }
                    }
                }
                if(tmpResOfOne == null || tmpResOfOne.size() == 0)
                    continue;
                res.add(tmpResOfOne);
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    在这里插入图片描述

  • 相关阅读:
    Go HTTP 调用(上)
    javascript设计模式单例
    webrtc 安卓端多人视频会议
    算法刷题打卡第17天:全局倒置与局部倒置
    Zookeeper的api使用
    MySQL Cluster
    Android配置环境
    穿越障碍:最小路径和的高效算法比较【python力扣题64】
    深度剖析 —— 预处理
    二维数组的最小路径和问题
  • 原文地址:https://blog.csdn.net/xiangguang_fight/article/details/136137694