-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPrintTreesInLines.java
78 lines (71 loc) · 2.35 KB
/
PrintTreesInLines.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
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/**
* Created by Yang on 2017/4/25.
* 把二叉树打印成多行:
*
* 从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层打印到一行。
*/
public class PrintTreesInLines {
public ArrayList<ArrayList<Integer>> print(TreeNode pRoot) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(pRoot == null) {
return res;
}
Queue<TreeNode> nodes = new LinkedList<>();
nodes.add(pRoot);
ArrayList<Integer> currentLevel = new ArrayList<>();
int toBePrinted = 1;
int nextLevel = 0;
while(!nodes.isEmpty()) {
TreeNode node = nodes.peek();
currentLevel.add(node.val);
if(node.left != null) {
nodes.add(node.left);
nextLevel++;
}
if(node.right != null) {
nodes.add(node.right);
nextLevel++;
}
nodes.poll();
toBePrinted--;
if(toBePrinted == 0) {
res.add(new ArrayList<>(currentLevel));
currentLevel.clear();
toBePrinted = nextLevel;
nextLevel = 0;
}
}
return res;
}
public static void main(String[] args) {
PrintTreesInLines printTreesInLines = new PrintTreesInLines();
TreeNode tree = new TreeNode(8);
tree.left = new TreeNode(6);
tree.right = new TreeNode(10);
tree.left.left = new TreeNode(5);
tree.left.right = new TreeNode(7);
tree.right.left = new TreeNode(9);
tree.right.right = new TreeNode(11);
System.out.println(printTreesInLines.print(tree));
}
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
public String toString() {
if(left != null && right != null) {
return "[" + left + "," + val + "," + right + "]";
} else if(left != null) {
return "[" + left + "," + val + ",#,]";
} else if(right != null) {
return "[#," + val + "," + right + "]";
} else {
return "" + val;
}
}
}
}