forked from Wang-Jun-Chao/coding-interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test23.java
138 lines (127 loc) · 3.92 KB
/
Test23.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.util.*;
/**
* Author: 王俊超
* Date: 2015-04-24
* Time: 10:39
* Declaration: All Rights Reserved !!!
*/
public class Test23 {
/**
* 二叉树的树结点
*/
public static class BinaryTreeNode {
int value;
BinaryTreeNode left;
BinaryTreeNode right;
}
/**
* 从上往下打印出二叉树的每个结点,向一层的结点按照从左往右的顺序打印。
* 例如下的二叉树,
* 8
* / \
* 6 10
* / \ / \
* 5 7 9 11
* 则依次打印出8、6、10、5、3 、9、11.
*
* @param root 树的结点
*/
public static void printFromToBottom(BinaryTreeNode root) {
// 当结点非空时才进行操作
if (root != null) {
// 用于存放还未遍历的元素
Queue<BinaryTreeNode> list = new LinkedList<>();
// 将根结点入队
list.add(root);
// 用于记录当前处理的结点
BinaryTreeNode curNode;
// 队列非空则进行处理
while (!list.isEmpty()) {
// 删除队首元素
curNode = list.remove();
// 输出队首元素的值
System.out.print(curNode.value + " ");
// 如果左子结点不为空,则左子结点入队
if (curNode.left != null) {
list.add(curNode.left);
}
// 如果右子结点不为空,则左子结点入队
if (curNode.right != null) {
list.add(curNode.right);
}
}
}
}
public static void main(String[] args) {
// 8
// / \
// 6 10
// / \ / \
// 5 7 9 11
BinaryTreeNode root = new BinaryTreeNode();
root.value = 8;
root.left = new BinaryTreeNode();
root.left.value = 6;
root.left.left = new BinaryTreeNode();
root.left.left.value = 5;
root.left.right = new BinaryTreeNode();
root.left.right.value = 7;
root.right = new BinaryTreeNode();
root.right.value = 10;
root.right.left = new BinaryTreeNode();
root.right.left.value = 9;
root.right.right = new BinaryTreeNode();
root.right.right.value = 11;
printFromToBottom(root);
// 1
// /
// 3
// /
// 5
// /
// 7
// /
// 9
BinaryTreeNode root2 = new BinaryTreeNode();
root2.value = 1;
root2.left = new BinaryTreeNode();
root2.left.value = 3;
root2.left.left = new BinaryTreeNode();
root2.left.left.value = 5;
root2.left.left.left = new BinaryTreeNode();
root2.left.left.left.value = 7;
root2.left.left.left.left = new BinaryTreeNode();
root2.left.left.left.left.value = 9;
System.out.println("\n");
printFromToBottom(root2);
// 0
// \
// 2
// \
// 4
// \
// 6
// \
// 8
BinaryTreeNode root3 = new BinaryTreeNode();
root3.value = 0;
root3.right = new BinaryTreeNode();
root3.right.value = 2;
root3.right.right = new BinaryTreeNode();
root3.right.right.value = 4;
root3.right.right.right = new BinaryTreeNode();
root3.right.right.right.value = 6;
root3.right.right.right.right = new BinaryTreeNode();
root3.right.right.right.right.value = 8;
System.out.println("\n");
printFromToBottom(root3);
// 1
BinaryTreeNode root4 = new BinaryTreeNode();
root4.value = 1;
System.out.println("\n");
printFromToBottom(root4);
// null
System.out.println("\n");
printFromToBottom(null);
}
}