-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHierarchicalTrees60.java
More file actions
98 lines (89 loc) · 2.82 KB
/
HierarchicalTrees60.java
File metadata and controls
98 lines (89 loc) · 2.82 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
93
94
95
96
97
98
package offer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
/*
* 层次打印二叉树
*/
public class HierarchicalTrees60 {
/**
* 以前写的方法一;队列中每一层的节点后面加一个-1,表示此层的结束
* @param node
*/
public static void HierarchicalOrder(TreeNode23 node) {
if (node == null) {
return;
}
Queue<TreeNode23> q = new LinkedList<TreeNode23>();
q.add(node);
TreeNode23 t = new TreeNode23(-1);
q.add(t);
while (!q.isEmpty()) {
TreeNode23 tmp = q.poll();
if (tmp.data != -1) {
System.out.print(tmp.data + " ");
}
if (tmp.left != null) {
q.add(tmp.left);
}
if (tmp.right != null) {
q.add(tmp.right);
}
//如果碰到了队列的队头是 -1说明,该弹出-1,且换行
//并在队列的尾部添加-1,表示该层的节点都在队列中。-1为结束标志
if (!q.isEmpty() && q.peek().data == -1) {
q.poll();
q.add(t);
System.out.println();
}
}
}
/**
* 方法二就是需要
* @param pRoot:
* @return
*/
public ArrayList<ArrayList<Integer> > Print(TreeNode23 pRoot) {
ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> layerList = new ArrayList<Integer>();
Queue<TreeNode23> q = new LinkedList<TreeNode23>();
q.add(pRoot);
//当前层的节点数目
int printed = 1;
//记录节点的所有下层的节点数目
int nextLevel = 0;
if(pRoot == null){
return arr;
}
while(!q.isEmpty()){
TreeNode23 node = q.poll();
layerList.add(node.data);
if(node.left != null){
q.add(node.left);
nextLevel++;
}
if(node.right != null){
q.add(node.right);
nextLevel++;
}
--printed;
/*
* 队列中上一层的每个节点已经访问完毕(出队),打印
*/
if(printed == 0){
arr.add(layerList);
layerList = new ArrayList<Integer>();
printed = nextLevel;
nextLevel = 0;
}
}
return arr;
}
public static void main(String[] args) {
int[] a = { 8, 6, 10, 5,7,9,11};
TreeNode23 root = null;
root = new TreeOperate23().createSquence(a,0);
ArrayList<ArrayList<Integer>> list = new HierarchicalTrees60().Print(root);
System.out.println(list);
}
}