-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode.java
More file actions
41 lines (35 loc) · 974 Bytes
/
Copy pathTreeNode.java
File metadata and controls
41 lines (35 loc) · 974 Bytes
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
package tree;
import java.util.ArrayList;
import java.util.List;
public class TreeNode {
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
int value;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) {
value = x;
}
public void print() {
System.out.print(this.value + " ");
List<TreeNode> list = new ArrayList<TreeNode>();
list.add(this.left);
list.add(this.right);
List<TreeNode> nextList = null;
while (list != null && list.size() > 0) {
nextList = new ArrayList<TreeNode>();
for (TreeNode node : list) {
if (node != null) {
System.out.print(node.value + " ");
nextList.add(node.left);
nextList.add(node.right);
}
}
list = nextList;
}
}
}