友情支持
如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜
有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。
公众号的微信号是: jikerizhi 。因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。 |
102. Binary Tree Level Order Traversal
这个题其实很简单,只要保持需要读取值的那一层的节点就可以了。
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] ]
思路分析
-
一刷
-
二刷
-
三刷
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
/**
* Runtime: 3 ms, faster than 5.15% of Java online submissions for Binary Tree Level Order Traversal.
*
* Memory Usage: 40.2 MB, less than 5.33% of Java online submissions for Binary Tree Level Order Traversal.
*/
public List<List<Integer>> levelOrder(TreeNode root) {
if (Objects.isNull(root)) {
return Collections.emptyList();
}
List<List<Integer>> result = new ArrayList<>();
List<TreeNode> level = new ArrayList<>();
level.add(root);
while (!level.isEmpty()) {
List<TreeNode> temp = new ArrayList<>();
List<Integer> values = new ArrayList<>(level.size());
for (TreeNode node : level) {
values.add(node.val);
if (Objects.nonNull(node.left)) {
temp.add(node.left);
}
if (Objects.nonNull(node.right)) {
temp.add(node.right);
}
}
result.add(values);
level = temp;
}
return result;
}
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
/**
* Runtime: 3 ms, faster than 5.15% of Java online submissions for Binary Tree Level Order Traversal.
* <p>
* Memory Usage: 40.2 MB, less than 5.33% of Java online submissions for Binary Tree Level Order Traversal.
*
* @author D瓜哥 · https://www.diguage.com
* @since 2024-06-25 11:27
*/
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) {
return result;
}
Queue<TreeNode> topQueue = new LinkedList<>();
topQueue.add(root);
while (!topQueue.isEmpty()) {
List<Integer> level = new ArrayList<>();
Queue<TreeNode> bottomQueue = new LinkedList<>();
while (!topQueue.isEmpty()) {
TreeNode top = topQueue.poll();
level.add(top.val);
if (top.left != null) {
bottomQueue.add(top.left);
}
if (top.right != null) {
bottomQueue.add(top.right);
}
}
result.add(level);
topQueue = bottomQueue;
}
return result;
}
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
/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-06-25 11:27
*/
public List<List<Integer>> levelOrder(TreeNode root) {
if (root == null) {
return Collections.emptyList();
}
List<List<Integer>> result = new ArrayList<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> nums = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
nums.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
result.add(nums);
}
return result;
}
TODO:还可以优化成只用一个队列对象,思考如何实现? |