-
Notifications
You must be signed in to change notification settings - Fork 0
/
Display.java
65 lines (58 loc) · 1.4 KB
/
Display.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
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
package leetCode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* @className Display.java
* @author AT
* @version Create Time:2019年10月26日 下午3:32:54
* @param <E>
* @question: leetcode.p
* @describe:
*/
public class Display<E> {
public void doubleListDisplay(List<List<E>> dList) {
for (List<E> list : dList) {
System.out.print("[");
for (E item : list) {
System.out.print(item + ",");
}
System.out.println("]");
}
}
private String[] levelTraversal(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
List<String> treeList = new ArrayList<String>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode crt = queue.poll();
if (crt != null) {
treeList.add(crt.val + "");
if (crt.left != null) {
queue.offer(crt.left);
} else {
if (crt.right != null)
queue.offer(null);
}
if (crt.right != null) {
queue.offer(crt.right);
} else {
if (crt.left != null)
queue.offer(null);
}
} else {
treeList.add("null");
}
}
}
return (String[]) treeList.toArray(new String[treeList.size()]);
}
public void treeDisplay(TreeNode root) {
String[] rstArr = levelTraversal(root);
System.out.println(Arrays.toString(rstArr));
}
}