Easy
32635FavoriteShare
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:
- The depth of the tree is at most 1000.
- The total number of nodes is at most 5000.
BFS法:
C++:
/*
* @Autor: SourDumplings
* @Date: 2019-09-25 09:42:58
* @Link: https://github.com/SourDumplings/
* @Email: changzheng300@foxmail.com
* @Description: https://leetcode.com/problems/n-ary-tree-level-order-traversal/
*/
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution
{
public:
vector<vector<int>> levelOrder(Node *root)
{
vector<vector<int>> res;
if (!root)
{
return res;
}
doLevelOrder(res, root);
return res;
}
void doLevelOrder(vector<vector<int>> &res, Node *root)
{
queue<Node *> thisQ;
thisQ.push(root);
while (!thisQ.empty())
{
vector<int> thisLevel;
queue<Node *> nextQ;
while (!thisQ.empty())
{
Node *p = thisQ.front();
thisQ.pop();
thisLevel.push_back(p->val);
for (Node *c : p->children)
{
nextQ.push(c);
}
}
thisQ = std::move(nextQ);
res.push_back(thisLevel);
}
}
};
Java:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/*
* @Autor: SourDumplings
* @Date: 2019-09-25 11:10:59
* @Link: https://github.com/SourDumplings/
* @Email: changzheng300@foxmail.com
* @Description: https://leetcode.com/problems/n-ary-tree-level-order-traversal/
*/
/*
// 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<List<Integer>>();
if (root == null)
{
return res;
}
doLevelOrder(res, root);
return res;
}
public void doLevelOrder(List<List<Integer>> res, Node root)
{
Queue<Node> thisQ = new LinkedList<>();
thisQ.add(root);
while (!thisQ.isEmpty())
{
Queue<Node> nextQ = new LinkedList<>();
List<Integer> thisLevel = new ArrayList<>();
while (!thisQ.isEmpty())
{
Node p = thisQ.poll();
thisLevel.add(p.val);
for (Node c : p.children)
{
nextQ.add(c);
}
}
res.add(thisLevel);
thisQ = nextQ;
}
}
}
DFS法:
Java:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/*
* @Autor: SourDumplings
* @Date: 2019-09-25 11:27:52
* @Link: https://github.com/SourDumplings/
* @Email: changzheng300@foxmail.com
* @Description: https://leetcode.com/problems/n-ary-tree-level-order-traversal/
*/
/*
// 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<List<Integer>>();
if (root == null)
{
return res;
}
doLevelOrder(res, root, 1);
return res;
}
public void doLevelOrder(List<List<Integer>> res, Node root, int level)
{
if (res.size() < level)
{
res.add(new ArrayList<Integer>());
}
res.get(level - 1).add(root.val);
for (Node c : root.children)
{
doLevelOrder(res, c, level + 1);
}
}
}