Skip to content

Commit 16f168e

Browse files
add 1485
1 parent 4884f70 commit 16f168e

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ _If you like this project, please leave me a star._ ★
7878
|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|
7979
|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|
8080
|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|
81+
|1485|[Clone Binary Tree With Random Pointer](https://leetcode.com/problems/clone-binary-tree-with-random-pointer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1485.java) | |Medium|HashTable, Tree, DFS, BFS|
8182
|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|
8283
|1480|[Running Sum of 1d Array](https://leetcode.com/problems/running-sum-of-1d-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1480.java), [C++](../master/cpp/_1480.cpp)| |Easy|Array|
8384
|1476|[Subrectangle Queries](https://leetcode.com/problems/subrectangle-queries/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1476.java) | |Medium|Array|
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class _1485 {
7+
public static class Solution1 {
8+
public NodeCopy copyRandomBinaryTree(Node root) {
9+
if (root == null) {
10+
return null;
11+
}
12+
Map<Node, NodeCopy> map = new HashMap<>();
13+
map.put(root, new NodeCopy(root.val));
14+
dfs(root, map);
15+
dfsAgain(root, map);
16+
return map.get(root);
17+
}
18+
19+
private void dfsAgain(Node root, Map<Node, NodeCopy> map) {
20+
if (root == null) {
21+
return;
22+
}
23+
NodeCopy copy = map.get(root);
24+
if (root.left != null) {
25+
copy.left = map.get(root.left);
26+
}
27+
if (root.right != null) {
28+
copy.right = map.get(root.right);
29+
}
30+
if (root.random != null) {
31+
copy.random = map.get(root.random);
32+
}
33+
map.put(root, copy);
34+
dfsAgain(root.left, map);
35+
dfsAgain(root.right, map);
36+
}
37+
38+
private void dfs(Node root, Map<Node, NodeCopy> map) {
39+
if (root == null) {
40+
return;
41+
}
42+
NodeCopy copy = map.getOrDefault(root, new NodeCopy(root.val));
43+
if (root.left != null) {
44+
copy.left = new NodeCopy(root.left.val);
45+
}
46+
if (root.right != null) {
47+
copy.right = new NodeCopy(root.right.val);
48+
}
49+
if (root.random != null) {
50+
copy.random = new NodeCopy(root.random.val);
51+
}
52+
map.put(root, copy);
53+
dfs(root.left, map);
54+
dfs(root.right, map);
55+
}
56+
}
57+
58+
public static class Node {
59+
int val;
60+
public Node left;
61+
public Node right;
62+
public Node random;
63+
64+
public Node() {
65+
}
66+
67+
;
68+
69+
public Node(int val) {
70+
this.val = val;
71+
}
72+
73+
;
74+
75+
public Node(int val, Node left, Node right, Node random) {
76+
this.val = val;
77+
this.left = left;
78+
this.right = right;
79+
this.random = random;
80+
}
81+
}
82+
83+
public static class NodeCopy {
84+
int val;
85+
public NodeCopy left;
86+
public NodeCopy right;
87+
public NodeCopy random;
88+
89+
public NodeCopy() {
90+
}
91+
92+
;
93+
94+
public NodeCopy(int val) {
95+
this.val = val;
96+
}
97+
98+
;
99+
100+
public NodeCopy(int val, NodeCopy left, NodeCopy right, NodeCopy random) {
101+
this.val = val;
102+
this.left = left;
103+
this.right = right;
104+
this.random = random;
105+
}
106+
}
107+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1485;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
public class _1485Test {
8+
9+
private static _1485.Solution1 solution1;
10+
11+
@BeforeClass
12+
public static void setup() {
13+
solution1 = new _1485.Solution1();
14+
}
15+
16+
@Test
17+
public void test1() {
18+
_1485.Node root = new _1485.Node(1);
19+
_1485.Node node1 = new _1485.Node(4);
20+
_1485.Node node2 = new _1485.Node(7);
21+
root.right = node1;
22+
node1.left = node2;
23+
node1.random = node2;
24+
node2.random = root;
25+
_1485.NodeCopy actual = solution1.copyRandomBinaryTree(root);
26+
System.out.println("Finished.");
27+
}
28+
29+
}

0 commit comments

Comments
 (0)