forked from yunyinl/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeLevelOrderTraversal.java
More file actions
73 lines (67 loc) · 2.07 KB
/
Copy pathBinaryTreeLevelOrderTraversal.java
File metadata and controls
73 lines (67 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package amazon;
// 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]
// ]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import entity.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* leetcode 102 https://leetcode.com/problems/binary-tree-level-order-traversal
* 二叉树的层次遍历,其实也就是图的宽度优先遍历,记住使用数据结构Queue
* 两层循环:
* 外层:while 队列不为空
* 内层:while 队列元素大小的所有元素全部出队,打印该层元素;把下层元素入队
*
* 扩展练习:https://leetcode.com/problems/binary-tree-level-order-traversal-ii
* Created by anduo on 17-3-13.
*/
public class BinaryTreeLevelOrderTraversal {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) return result;
// bfs 算法
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
TreeNode node;
while (!queue.isEmpty()) {
List<Integer> levels = new ArrayList<>();
int nums = queue.size();
while (nums-- > 0) {
// 把该层的所有节点放到levels中,并且把这层的下级节点依次放入队列中
node = queue.poll();
levels.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
// 把该层节点的list加到result中
result.add(levels);
}
return result;
}
}