Skip to content

Commit 83a0db6

Browse files
committed
fd
1 parent e17c379 commit 83a0db6

File tree

2 files changed

+51
-41
lines changed

2 files changed

+51
-41
lines changed
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,48 @@
1+
import java.util.ArrayList;
12
import java.util.LinkedList;
23
import java.util.List;
34
import java.util.Queue;
45

56
public class BinaryTreeLevelOrderTraversalII {
67

78
// 耗时2ms,和I一样,只不过加到result时添加到头
8-
public List<List<Integer>> levelOrder(TreeNode root) {
9-
List<List<Integer>> result = new LinkedList<List<Integer>>();
9+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
10+
List<List<Integer>> result = new LinkedList<>();
1011

1112
if (root == null) {
1213
return result;
1314
}
1415

15-
Queue<TreeNode> queue = new LinkedList<TreeNode>();
16-
Queue<TreeNode> next = new LinkedList<TreeNode>();
17-
queue.add(root);
16+
Queue<TreeNode> queue = new LinkedList<>();
17+
Queue<TreeNode> next = new LinkedList<>();
1818

19-
List<Integer> cur = null;
19+
List<Integer> list = null;
20+
21+
queue.offer(root);
2022

2123
while (!queue.isEmpty()) {
2224
TreeNode node = queue.poll();
2325

24-
if (cur == null) {
25-
cur = new LinkedList<Integer>();
26-
result.add(0, cur);
26+
if (list == null) {
27+
list = new ArrayList<>();
2728
}
2829

29-
cur.add(node.val);
30+
list.add(node.val);
3031

3132
if (node.left != null) {
32-
next.add(node.left);
33+
next.offer(node.left);
3334
}
34-
3535
if (node.right != null) {
36-
next.add(node.right);
36+
next.offer(node.right);
3737
}
3838

3939
if (queue.isEmpty()) {
40-
Queue<TreeNode> temp = queue;
41-
queue = next;
42-
next = temp;
43-
cur = null; // 注意这里要置空
40+
queue.addAll(next);
41+
next.clear();
42+
result.add(0, list);
43+
list = null;
4444
}
4545
}
46-
4746
return result;
4847
}
4948
}

leetcode/src/Main.java

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,44 @@
44

55
public class Main {
66

7-
public int minDepth(TreeNode root) {
7+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
8+
List<List<Integer>> result = new LinkedList<>();
9+
810
if (root == null) {
9-
return 0;
10-
}
11-
if (root.left == null && root.right == null) {
12-
return 1;
13-
}
14-
if (root.left == null) {
15-
return minDepth(root.right) + 1;
16-
}
17-
if (root.right == null) {
18-
return minDepth(root.left) + 1;
11+
return result;
1912
}
20-
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
21-
}
2213

23-
public int minDepth(TreeNode root, int depth) {
24-
if (root.left == null && root.right == null) {
25-
return depth;
26-
}
27-
if (root.left == null) {
28-
return minDepth(root.right, depth + 1);
29-
}
30-
if (root.right == null) {
31-
return minDepth(root.left, depth + 1);
14+
Queue<TreeNode> queue = new LinkedList<>();
15+
Queue<TreeNode> next = new LinkedList<>();
16+
17+
List<Integer> list = null;
18+
19+
queue.offer(root);
20+
21+
while (!queue.isEmpty()) {
22+
TreeNode node = queue.poll();
23+
24+
if (list == null) {
25+
list = new ArrayList<>();
26+
}
27+
28+
list.add(node.val);
29+
30+
if (node.left != null) {
31+
next.offer(node.left);
32+
}
33+
if (node.right != null) {
34+
next.offer(node.right);
35+
}
36+
37+
if (queue.isEmpty()) {
38+
queue.addAll(next);
39+
next.clear();
40+
result.add(0, list);
41+
list = null;
42+
}
3243
}
33-
return Math.min(minDepth(root.left, depth + 1), minDepth(root.right, depth + 1));
44+
return result;
3445
}
3546

3647
public static void main(String[] args) {

0 commit comments

Comments
 (0)