forked from javadev/LeetCode-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.java
More file actions
54 lines (48 loc) · 1.42 KB
/
Copy pathNode.java
File metadata and controls
54 lines (48 loc) · 1.42 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
package com_github_leetcode;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("java:S1104")
public class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<>();
}
public Node(int val) {
this.val = val;
neighbors = new ArrayList<>();
}
public Node(int val, List<Node> neighbors) {
this.val = val;
this.neighbors = neighbors;
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("[");
for (int i = 0; i < neighbors.size(); i++) {
Node node = neighbors.get(i);
if (i > 0) {
result.append(",");
}
if (node.neighbors.isEmpty()) {
result.append(node.val);
} else {
StringBuilder result2 = new StringBuilder();
result2.append("[");
for (int j = 0; j < node.neighbors.size(); j++) {
Node nodeItem = node.neighbors.get(j);
if (j > 0) {
result2.append(",");
}
result2.append(nodeItem.val);
}
result2.append("]");
result.append(result2.toString());
}
}
result.append("]");
return result.toString();
}
}