Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit16f168e

Browse files
add 1485
1 parent4884f70 commit16f168e

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+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.HashMap;
4+
importjava.util.Map;
5+
6+
publicclass_1485 {
7+
publicstaticclassSolution1 {
8+
publicNodeCopycopyRandomBinaryTree(Noderoot) {
9+
if (root ==null) {
10+
returnnull;
11+
}
12+
Map<Node,NodeCopy>map =newHashMap<>();
13+
map.put(root,newNodeCopy(root.val));
14+
dfs(root,map);
15+
dfsAgain(root,map);
16+
returnmap.get(root);
17+
}
18+
19+
privatevoiddfsAgain(Noderoot,Map<Node,NodeCopy>map) {
20+
if (root ==null) {
21+
return;
22+
}
23+
NodeCopycopy =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+
privatevoiddfs(Noderoot,Map<Node,NodeCopy>map) {
39+
if (root ==null) {
40+
return;
41+
}
42+
NodeCopycopy =map.getOrDefault(root,newNodeCopy(root.val));
43+
if (root.left !=null) {
44+
copy.left =newNodeCopy(root.left.val);
45+
}
46+
if (root.right !=null) {
47+
copy.right =newNodeCopy(root.right.val);
48+
}
49+
if (root.random !=null) {
50+
copy.random =newNodeCopy(root.random.val);
51+
}
52+
map.put(root,copy);
53+
dfs(root.left,map);
54+
dfs(root.right,map);
55+
}
56+
}
57+
58+
publicstaticclassNode {
59+
intval;
60+
publicNodeleft;
61+
publicNoderight;
62+
publicNoderandom;
63+
64+
publicNode() {
65+
}
66+
67+
;
68+
69+
publicNode(intval) {
70+
this.val =val;
71+
}
72+
73+
;
74+
75+
publicNode(intval,Nodeleft,Noderight,Noderandom) {
76+
this.val =val;
77+
this.left =left;
78+
this.right =right;
79+
this.random =random;
80+
}
81+
}
82+
83+
publicstaticclassNodeCopy {
84+
intval;
85+
publicNodeCopyleft;
86+
publicNodeCopyright;
87+
publicNodeCopyrandom;
88+
89+
publicNodeCopy() {
90+
}
91+
92+
;
93+
94+
publicNodeCopy(intval) {
95+
this.val =val;
96+
}
97+
98+
;
99+
100+
publicNodeCopy(intval,NodeCopyleft,NodeCopyright,NodeCopyrandom) {
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+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1485;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
publicclass_1485Test {
8+
9+
privatestatic_1485.Solution1solution1;
10+
11+
@BeforeClass
12+
publicstaticvoidsetup() {
13+
solution1 =new_1485.Solution1();
14+
}
15+
16+
@Test
17+
publicvoidtest1() {
18+
_1485.Noderoot =new_1485.Node(1);
19+
_1485.Nodenode1 =new_1485.Node(4);
20+
_1485.Nodenode2 =new_1485.Node(7);
21+
root.right =node1;
22+
node1.left =node2;
23+
node1.random =node2;
24+
node2.random =root;
25+
_1485.NodeCopyactual =solution1.copyRandomBinaryTree(root);
26+
System.out.println("Finished.");
27+
}
28+
29+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp