102. Binary Tree Level Order Traversal (Medium)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree[3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
Solution 1: BFS O(n); O(w)
/**
* BFS O(n);O(w)
* Since each time we need to traverse each level of the tree, we can use BFS to solve this problem.
* 1.Use Queue to store nodes of each level.
* 2.Traverse one level at each iteration instead of regular BFS, since result needs to be stored in List-List.
* 3.By getting the size of the queue, we know how many nodes in current level.
* 4.If left or right child of current node is not null, add it to the queue.
* 5.Add value of nodes of each level into the result.
*/
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
// 1.add start node into queue
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
// 2.while queue is not empty
while (!queue.isEmpty()) {
// 3.level x -> x + 1
List<Integer> curLevel = new ArrayList<>(); //can be replaced by deep copy, but will lead to low efficiency
int size = queue.size();
for (int i = 0; i < size; i++) { //level x
TreeNode node = queue.poll();
curLevel.add(node.val);
if (node.left != null) {
queue.add(node.left); //level x + 1
}
if (node.right != null) {
queue.add(node.right); //level x + 1
}
}
res.add(curLevel); // no deep copy
}
return res;
}
Solution 2: DFS O(n); O(h)
public List<List<Integer>> levelOrderB(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
dfs(res, root, 0);
return res;
}
private void dfs(List<List<Integer>> res, TreeNode root, int level) {
if (root == null) {
return;
}
if (res.size() <= level) {
res.add(new ArrayList<>());
}
res.get(level).add(root.val);
dfs(res, root.left, level + 1);
dfs(res, root.right, level + 1);
}
Follow Up:
1.打印出每层最大的两个数字,如果不到2个,就打印null.
public List<List<Integer>> levelOrderC(TreeNode root) { //BFS
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
// 1.add start node into queue
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
// 2.while queue is not empty
while (!queue.isEmpty()) {
// 3.level x -> x + 1
// List<Integer> curLevel = new ArrayList<>(); //can be replaced by deep copy, but will lead to low efficiency
int size = queue.size();
int max = Integer.MIN_VALUE;
int secondMax = Integer.MIN_VALUE;
for (int i = 0; i < size; i++) { //level x
TreeNode head = queue.poll();
// curLevel.add(head.val);
if (head.val > max) {
secondMax = max;
max = head.val;
} else if (head.val > secondMax) {
secondMax = head.val;
}
// System.out.print(head.val + " ");
if (head.left != null) {
queue.add(head.left); //level x + 1
}
if (head.right != null) {
queue.add(head.right); //level x + 1
}
}
System.out.println(max == Integer.MIN_VALUE ? "NULL" : max + " " + (secondMax == Integer.MIN_VALUE ? "NULL" : secondMax));
// res.add(curLevel); // no deep copy
// System.out.println(curLevel);
}
return res;
}