Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b538577

Browse files
add a solution for 1379
1 parent 59a2adb commit b538577

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
return getTargetCopy(original.right, cloned.right, target);
1919
}
2020
}
21+
22+
public static class Solution2 {
23+
/**
24+
* My completely original solution on 5/17/2022.
25+
*/
26+
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
27+
if (original == null || cloned == null) {
28+
return null;
29+
}
30+
if (original == target) {
31+
return cloned;
32+
}
33+
TreeNode left = getTargetCopy(original.left, cloned.left, target);
34+
if (left == null) {
35+
return getTargetCopy(original.right, cloned.right, target);
36+
}
37+
return left;
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
public class _1379Test {
1212
private static _1379.Solution1 solution1;
13+
private static _1379.Solution2 solution2;
1314
private static TreeNode original;
1415
private static TreeNode cloned;
1516
private static TreeNode target;
1617

1718
@BeforeClass
1819
public static void setup() {
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)