-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoffer32.java
More file actions
42 lines (39 loc) · 1.08 KB
/
offer32.java
File metadata and controls
42 lines (39 loc) · 1.08 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
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
/**
* [32-I] 从上到下打印二叉树
*
* 题目: 从上到下打印出二叉树的每个节点, 同一层的节点按照从左到右的顺序打印.
*
* 思路: 使用队列来进行层次遍历.
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
/**
* 时间复杂度: O(n)
* 空间复杂度: O(n)
*/
public int[] levelOrder(TreeNode root) {
if (root == null) return new int[0];
List<Integer> list = new ArrayList<>();
Deque<TreeNode> queue = new ArrayDeque<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
list.add(cur.val);
if (cur.left != null) queue.offer(cur.left);
if (cur.right != null) queue.offer(cur.right);
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
}