Skip to content

Commit 7abdc6e

Browse files
add 1490
1 parent 03e2748 commit 7abdc6e

File tree

4 files changed

+88
-4
lines changed

4 files changed

+88
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ _If you like this project, please leave me a star._ ★
6969
|1493|[Longest Subarray of 1's After Deleting One Element](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1493.java) |[:tv:](https://youtu.be/nKhteIRZ2Ok) |Medium|Array|
7070
|1492|[The kth Factor of n](https://leetcode.com/problems/the-kth-factor-of-n/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1492.java) | |Medium|Math|
7171
|1491|[Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1491.java) | |Easy|Array, Sort|
72+
|1490|[Clone N-ary Tree](https://leetcode.com/problems/clone-n-ary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1490.java) | |Medium|HashTable, Tree, DFS, BFS|
7273
|1487|[Making File Names Unique](https://leetcode.com/problems/making-file-names-unique/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1487.java) | |Medium|HashTable, String|
7374
|1486|[XOR Operation in an Array](https://leetcode.com/problems/xor-operation-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1486.java) | |Medium|Array, Bit Manipulation|
7475
|1481|[Least Number of Unique Integers after K Removals](https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1481.java) | |Medium|Array, Sort|

src/main/java/com/fishercoder/common/classes/Node.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
import java.util.ArrayList;
44
import java.util.List;
5+
import java.util.Objects;
56

67
public class Node {
78
public int val;
89
public List<Node> children;
910

1011
public Node() {
12+
this.children = new ArrayList<>();
1113
}
1214

13-
public Node(int val, List<Node> children) {
15+
public Node(int val) {
1416
this.val = val;
15-
this.children = children;
17+
this.children = new ArrayList<>();
1618
}
1719

18-
public Node(int val) {
20+
public Node(int val, List<Node> children) {
1921
this.val = val;
20-
this.children = new ArrayList<>();
22+
this.children = children;
2123
}
2224

2325
//todo: implement this method
@@ -28,4 +30,21 @@ public Node(int val) {
2830
public static Node createNaryTree(List<Integer> preorderValues) {
2931
return null;
3032
}
33+
34+
@Override
35+
public boolean equals(Object o) {
36+
if (this == o) {
37+
return true;
38+
}
39+
if (o == null || getClass() != o.getClass()) {
40+
return false;
41+
}
42+
Node node = (Node) o;
43+
return val == node.val && Objects.equals(children, node.children);
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
return Objects.hash(val, children);
49+
}
3150
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.Node;
4+
5+
import java.util.HashMap;
6+
import java.util.LinkedList;
7+
import java.util.Map;
8+
import java.util.Queue;
9+
10+
public class _1490 {
11+
public static class Solution1 {
12+
public Node cloneTree(Node root) {
13+
if (root == null) {
14+
return root;
15+
}
16+
Map<Node, Node> map = new HashMap<>();
17+
map.put(root, new Node(root.val));
18+
Queue<Node> queue = new LinkedList<>();
19+
queue.offer(root);
20+
while (!queue.isEmpty()) {
21+
Node curr = queue.poll();
22+
for (Node child : curr.children) {
23+
Node childCopy = new Node(child.val);
24+
if (!map.containsKey(child)) {
25+
map.put(child, childCopy);
26+
queue.offer(child);
27+
}
28+
map.get(curr).children.add(childCopy);
29+
}
30+
}
31+
return map.get(root);
32+
}
33+
}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.Node;
4+
import com.fishercoder.solutions._1490;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _1490Test {
11+
private static _1490.Solution1 solution1;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1490.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
Node root = new Node(8);
21+
Node child = new Node(1);
22+
root.children.add(child);
23+
Node leftGrandChild = new Node(8);
24+
Node rightGrandChild = new Node(5);
25+
child.children.add(leftGrandChild);
26+
child.children.add(rightGrandChild);
27+
assertEquals(root, solution1.cloneTree(root));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)