-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTreeLevelOrderTraversalII.java
More file actions
92 lines (83 loc) · 2.43 KB
/
BinaryTreeLevelOrderTraversalII.java
File metadata and controls
92 lines (83 loc) · 2.43 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class BinaryTreeLevelOrderTraversalII {
private static List<List<Integer>> res = new ArrayList<>();
public static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
/**
* 层次遍历
*
* @param root
* @return
*/
public static void levelTraversal(TreeNode root) {
if (root == null) return;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (queue.size() != 0) {
int len = queue.size();
for (int i = 0; i < len; i++) {
TreeNode temp = queue.poll();
System.out.println(temp.val + " ");
if (temp.left != null) queue.add(temp.left);
if (temp.right != null) queue.add(temp.right);
}
}
}
public static List<List<Integer>> levelOrderBottom(TreeNode root) {
if (root == null) return res;
levelDown(root, 0);
Collections.reverse(res);
return res;
}
private static void levelDown(TreeNode root, int level) {
if (root == null) return;
if (res.size() == level) {
List<Integer> temp = new ArrayList<>();
temp.add(root.val);
res.add(level, temp);
} else {
res.get(level).add(root.val);
}
levelDown(root.left, level + 1);
levelDown(root.right, level + 1);
return;
}
public static void main(String[] args) {
/**
*
* 3
* 9 20
* 15 7
*
*output:
*[
* [15,7],
* [9,20],
* [3]
*]
*
*
*/
// 初始化二叉树数据
TreeNode root = new TreeNode(3);
root.left = new TreeNode(9);
TreeNode rootRight = new TreeNode(20);
root.right = rootRight;
rootRight.left = new TreeNode(15);
rootRight.right = new TreeNode(7);
levelOrderBottom(root);
for (List<Integer> integerList : res) {
for (Integer integer : integerList) {
System.out.print(integer + " ");
}
}
}
}