zoukankan      html  css  js  c++  java
  • 429. N-ary Tree Level Order Traversal

    Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

    For example, given a 3-ary tree:

    We should return its level order traversal:

    [
         [1],
         [3,2,4],
         [5,6]
    ]
    

    Note:

    1. The depth of the tree is at most 1000.
    2. The total number of nodes is at most 5000.

    M1: BFS

    time: O(n), space: O(N)  -- N: 最多一层的节点数

    /*
    // Definition for a Node.
    class Node {
        public int val;
        public List<Node> children;
    
        public Node() {}
    
        public Node(int _val,List<Node> _children) {
            val = _val;
            children = _children;
        }
    };
    */
    class Solution {
        public List<List<Integer>> levelOrder(Node root) {
            List<List<Integer>> res = new ArrayList<>();
            Queue<Node> q = new LinkedList<>();
            if(root == null) {
                return res;
            }
            
            q.offer(root);
            while(!q.isEmpty()) {
                List<Integer> level = new ArrayList<>();
                int size = q.size();
                for(int i = 0; i < size; i++) {
                    Node tmp = q.poll();
                    level.add(tmp.val);
                    
                    if(tmp.children != null) {
                        for(Node n : tmp.children) {
                            q.offer(n);
                        }
                    }
                }
                res.add(level);
            }
            return res;
        }
    }

    M2: recursion

    time: O(n), space: O(height)

    /*
    // Definition for a Node.
    class Node {
        public int val;
        public List<Node> children;
    
        public Node() {}
    
        public Node(int _val,List<Node> _children) {
            val = _val;
            children = _children;
        }
    };
    */
    class Solution {
        public List<List<Integer>> levelOrder(Node root) {
            List<List<Integer>> res = new ArrayList<>();
            if(root == null) {
                return res;
            }
            levelOrder(root, 0, res);
            return res;
        }
        
        public void levelOrder(Node root, int level, List<List<Integer>> res) {
            if(root == null) {
                return;
            }
            if(res.size() == level) {
                res.add(new ArrayList<>());
            }
            res.get(level).add(root.val);
            
            if(root.children != null) {
                for(Node n : root.children) {
                    levelOrder(n, level + 1, res);
                }
            }
        }
    }
  • 相关阅读:
    VS2015前端工具:NPM和Web Essentials
    数组模拟实现邻接表
    面经
    PyCharm 使用简介
    DbModel
    网站静态化处理—web前端优化—下【终篇】(13)
    TCP通信中的大文件传送
    网站静态化处理—web前端优化—中(12)
    API帮助页面
    客户端程序通过TCP通信传送"小文件"到服务器
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10193633.html
Copyright © 2011-2022 走看看