Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //BFS solution
- //Time: O(n), 6ms
- //Space: O(n)
- class Solution {
- public List<List<Integer>> levelOrder(Node root) {
- List<List<Integer>> ans = new ArrayList<>();
- if(root == null) return ans;
- Queue<Node> q = new LinkedList<>();
- q.offer(root);
- while(!q.isEmpty()) {
- int size = q.size();
- List<Integer> list = new ArrayList<>();
- for(int i = 0; i < size; i++) {
- Node node = q.poll();
- list.add(node.val);
- for(Node child : node.children) {
- q.offer(child);
- }
- }
- ans.add(list);
- }
- return ans;
- }
- }
- /*
- // 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;
- }
- };
- */
Add Comment
Please, Sign In to add comment