-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathLevelOrderBottom.java
34 lines (31 loc) · 1.1 KB
/
LevelOrderBottom.java
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
package com.leetcode.easy;
import com.leetcode.base.TreeNode;
import java.util.*;
public class LevelOrderBottom {
//这道题目和树的层序遍历思想一致 只不过最后将集合顺序逆序一下就好
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int n = queue.size();//当前层有几个结点 由于队列的长度在不断变化 使用n来做一个记录
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
TreeNode node = queue.remove();
list.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
res.add(list);
}
Collections.reverse(res);
return res;
}
}