LeetCode 429. N-ary Tree Level Order Traversal

原题链接在这里:https://leetcode.com/problems/n-ary-tree-level-order-traversal/

题目:

Given an n-ary tree, return the level order traversal of its nodes' values.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

Example 1:

Input: root = [1,null,3,2,4,null,5,6]
Output: [[1],[3,2,4],[5,6]]

Example 2:

Input: 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]
Output: [[1],[2,3,4,5],[6,7,8,9,10],[11,12,13],[14]]

Constraints:

  • The height of the n-ary tree is less than or equal to 1000
  • The total number of nodes is between [0, 10^4]

题解:

When iterating by BFS, for current level nodes, get the size of queue. Iterate each of them.

Add node's value to item and if any of its children is not null, add them to queue.

Time Complexity: O(V+E). V is node count. E is edge count.

Space: O(V).

AC Java:

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4     public int val;
 5     public List<Node> children;
 6 
 7     public Node() {}
 8 
 9     public Node(int _val) {
10         val = _val;
11     }
12 
13     public Node(int _val, List<Node> _children) {
14         val = _val;
15         children = _children;
16     }
17 };
18 */
19 
20 class Solution {
21     public List<List<Integer>> levelOrder(Node root) {
22         List<List<Integer>> res = new ArrayList<>();
23         if(root == null){
24             return res;
25         }
26         
27         LinkedList<Node> que = new LinkedList<>();
28         que.add(root);
29         while(!que.isEmpty()){
30             int size = que.size();
31             List<Integer> item = new ArrayList<>();
32             while(size-- > 0){
33                 Node cur = que.poll();
34                 item.add(cur.val);
35                 for(Node node : cur.children){
36                     if(node != null){
37                         que.add(node);
38                     }
39                 }
40             }
41             
42             res.add(item);
43         }
44         
45         return res;
46     }
47 }

AC C++:

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4 public:
 5     int val;
 6     vector<Node*> children;
 7 
 8     Node() {}
 9 
10     Node(int _val) {
11         val = _val;
12     }
13 
14     Node(int _val, vector<Node*> _children) {
15         val = _val;
16         children = _children;
17     }
18 };
19 */
20 
21 class Solution {
22 public:
23     vector<vector<int>> levelOrder(Node* root) {
24         vector<vector<int>> res;
25         if(!root){
26             return res;
27         }
28 
29         queue<Node*> que;
30         que.push(root);
31 
32         while(!que.empty()){
33             int size = que.size();
34             vector<int> item;
35             while(size-- > 0){
36                 Node* cur = que.front();
37                 que.pop();
38                 item.push_back(cur->val);
39                 for(Node* child : cur->children){
40                     if(child){
41                         que.push(child);
42                     }
43                 }
44             }
45 
46             res.push_back(item);
47         }
48 
49         return res;
50     }
51 };

AC Python:

 1 """
 2 # Definition for a Node.
 3 class Node:
 4     def __init__(self, val=None, children=None):
 5         self.val = val
 6         self.children = children
 7 """
 8 
 9 class Solution:
10     def levelOrder(self, root: 'Node') -> List[List[int]]:
11         res = []
12         if not root:
13             return res
14         
15         que = deque([root])
16         while que:
17             size = len(que)
18             item = []
19             for i in range(size):
20                 cur = que.popleft()
21                 item.append(cur.val)
22                 for child in cur.children:
23                     if child:
24                         que.append(child)
25             
26             res.append(item)
27         return res
28         

类似Binary Tree Level Order TraversalN-ary Tree Preorder Traversal.

posted @ 2019-11-24 14:22  Dylan_Java_NYC  阅读(292)  评论(0编辑  收藏  举报