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

Commitb538577

Browse files
add a solution for 1379
1 parent59a2adb commitb538577

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_1379.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,23 @@ public final TreeNode getTargetCopy(final TreeNode original, final TreeNode clon
1818
returngetTargetCopy(original.right,cloned.right,target);
1919
}
2020
}
21+
22+
publicstaticclassSolution2 {
23+
/**
24+
* My completely original solution on 5/17/2022.
25+
*/
26+
publicfinalTreeNodegetTargetCopy(finalTreeNodeoriginal,finalTreeNodecloned,finalTreeNodetarget) {
27+
if (original ==null ||cloned ==null) {
28+
returnnull;
29+
}
30+
if (original ==target) {
31+
returncloned;
32+
}
33+
TreeNodeleft =getTargetCopy(original.left,cloned.left,target);
34+
if (left ==null) {
35+
returngetTargetCopy(original.right,cloned.right,target);
36+
}
37+
returnleft;
38+
}
39+
}
2140
}

‎src/test/java/com/fishercoder/_1379Test.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@
1010

1111
publicclass_1379Test {
1212
privatestatic_1379.Solution1solution1;
13+
privatestatic_1379.Solution2solution2;
1314
privatestaticTreeNodeoriginal;
1415
privatestaticTreeNodecloned;
1516
privatestaticTreeNodetarget;
1617

1718
@BeforeClass
1819
publicstaticvoidsetup() {
1920
solution1 =new_1379.Solution1();
21+
solution2 =new_1379.Solution2();
2022
}
2123

2224
@Test
@@ -25,6 +27,7 @@ public void test1() {
2527
cloned =TreeUtils.constructBinaryTree(Arrays.asList(7,4,3,null,null,6,19));
2628
target =TreeUtils.constructBinaryTree(Arrays.asList(3,6,19));
2729
TreeUtils.printBinaryTree(solution1.getTargetCopy(original,cloned,target));
30+
TreeUtils.printBinaryTree(solution2.getTargetCopy(original,cloned,target));
2831
}
2932

3033
@Test

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp