forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1379.java
40 lines (37 loc) · 1.32 KB
/
_1379.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
public class _1379 {
public static class Solution1 {
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
if (original == null) {
return null;
}
if (original.val == target.val) {
return cloned;
}
TreeNode left = getTargetCopy(original.left, cloned.left, target);
if (left != null && left.val == target.val) {
return left;
}
return getTargetCopy(original.right, cloned.right, target);
}
}
public static class Solution2 {
/**
* My completely original solution on 5/17/2022.
*/
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
if (original == null || cloned == null) {
return null;
}
if (original == target) {
return cloned;
}
TreeNode left = getTargetCopy(original.left, cloned.left, target);
if (left == null) {
return getTargetCopy(original.right, cloned.right, target);
}
return left;
}
}
}