-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
58 lines (49 loc) · 1.17 KB
/
Node.java
File metadata and controls
58 lines (49 loc) · 1.17 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
import java.util.*;
public class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
public String toString() {
return toString(0);
}
public String toString(int q) {
String spaces = "";
for(int i = 0; i < q; i++) {
spaces += " ";
}
String ret = spaces + val + " ... ";
if(null == children) {
ret += "x";
}
else {
ret += "[ ";
for(Node node : children) {
ret += node.val + ", ";
}
ret += " ]";
for(Node node : children) {
ret += "\n";
ret += node.toString(q + 2);
}
}
return ret;
}
public static void main(String[] args) {
Node leaf1 = new Node(5, null);
Node leaf2 = new Node(6, null);
List<Node> leftNodes = new ArrayList<>();
leftNodes.add(leaf1);
leftNodes.add(leaf2);
Node left = new Node(3, leftNodes);
List<Node> rootList = new ArrayList<>();
rootList.add(left);
rootList.add(new Node(2,null));
rootList.add(new Node(4,null));
Node root = new Node(1, rootList);
System.out.println(root);
}
}