Topic
- Tree
- Breadth-first Search
- Depth-first Search
Description
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 , 1 0 4 ] [0, 10^4] [0,104]
Analysis
方法一:BFS
方法二:DFS
Submission
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class NAryTreeLevelOrderTraversal {
public static class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
}
//方法一:BFS
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> result = new ArrayList<>();
if(root == null)
return result;
LinkedList<Node> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
List<Integer> level = new ArrayList<>();
for(int size = queue.size(); size > 0; size--) {
Node node = queue.poll();
level.add(node.val);
if(node.children != null && !node.children.isEmpty())
queue.addAll(node.children);
}
result.add(level);
}
return result;
}
//方法二:DFS
public List<List<Integer>> levelOrder2(Node root) {
List<List<Integer>> result = new ArrayList<>();
dfs(root, result, 0);
return result;
}
private void dfs(Node node, List<List<Integer>> result, int level) {
if(node == null)
return;
if(result.size() == level) {
List<Integer> temp = new ArrayList<>();
temp.add(node.val);
result.add(temp);
}else {
result.get(level).add(node.val);
}
if(node.children != null ) {
for(Node n : node.children)
dfs(n, result, level + 1);
}
}
}
Test
import static org.junit.Assert.*;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import java.util.Arrays;
import java.util.List;
import org.hamcrest.collection.IsEmptyCollection;
import org.junit.Test;
import com.lun.medium.NAryTreeLevelOrderTraversal.Node;
public class NAryTreeLevelOrderTraversalTest {
@Test
@SuppressWarnings("unchecked")
public void test() {
NAryTreeLevelOrderTraversal obj = new NAryTreeLevelOrderTraversal();
Node root = n(1, al(n(3, al(n(5), n(6))),
n(2),n(4)));
assertThat(obj.levelOrder(root), contains(al(1), al(3, 2, 4), al(5, 6)));
Node root2 = n(1, al(n(2), //
n(3, al(n(6),n(7, al(n(11, al(n(14))))))),//
n(4, al(n(8, al(n(12))))), //
n(5, al(n(9, al(n(13))), n(10)))));
assertThat(obj.levelOrder(root2), contains(al(1), al(2,3,4,5), //
al(6,7,8,9,10), al(11,12,13), al(14)));
assertThat(obj.levelOrder(null), IsEmptyCollection.empty());
}
@Test
@SuppressWarnings("unchecked")
public void test2() {
NAryTreeLevelOrderTraversal obj = new NAryTreeLevelOrderTraversal();
Node root = n(1, al(n(3, al(n(5), n(6))),
n(2),n(4)));
assertThat(obj.levelOrder2(root), contains(al(1), al(3, 2, 4), al(5, 6)));
Node root2 = n(1, al(n(2), //
n(3, al(n(6),n(7, al(n(11, al(n(14))))))),//
n(4, al(n(8, al(n(12))))), //
n(5, al(n(9, al(n(13))), n(10)))));
assertThat(obj.levelOrder2(root2), contains(al(1), al(2,3,4,5), //
al(6,7,8,9,10), al(11,12,13), al(14)));
assertThat(obj.levelOrder2(null), IsEmptyCollection.empty());
}
private Node n(int n) {
return new Node(n);
}
private Node n(int n, List<Node> children) {
return new Node(n, children);
}
@SuppressWarnings("unchecked")
private <T> List<T> al(T... a){
return Arrays.asList(a);
}
}