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);
                }
            }
        }
    }
  • 相关阅读:
    Lucene:(一)建立索引文件:2。建立索引文件(一)
    Lucene:(一)建立索引文件:2。建立索引文件(二)Segment文件
    92.外边距设置 Walker
    99.元素居中及样式重置 Walker
    94.外边距踩坑 Walker
    101.列表属性 Walker
    97.boxsizing属性 Walker
    98.溢出隐藏 Walker
    95.内边距设置 Walker
    96.内边距和边框踩坑 Walker
  • 原文地址:https://www.cnblogs.com/fatttcat/p/10193633.html
Copyright © 2011-2022 走看看