Skip to content

Commit 0fdc39d

Browse files
add 1379
1 parent abe2401 commit 0fdc39d

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1379|[Find a Corresponding Node of a Binary Tree in a Clone of That Tree](https://leetcode.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1379.java) | |Medium|Tree|
1112
|1377|[Frog Position After T Seconds](https://leetcode.com/problems/frog-position-after-t-seconds/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1377.java) | |Hard|DFS, BFS|
1213
|1376|[Time Needed to Inform All Employees](https://leetcode.com/problems/time-needed-to-inform-all-employees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1376.java) | |Medium|DFS|
1314
|1375|[Bulb Switcher III](https://leetcode.com/problems/bulb-switcher-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1375.java) | |Medium|Array|
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
7+
*
8+
* Given two binary trees original and cloned and given a reference to a node target in the original tree.
9+
* The cloned tree is a copy of the original tree.
10+
* Return a reference to the same node in the cloned tree.
11+
* Note that you are not allowed to change any of the two trees or the target node and the answer must be a reference to a node in the cloned tree.
12+
*
13+
* Follow up: Solve the problem if repeated values on the tree are allowed.
14+
*
15+
* Example 1:
16+
* Input: tree = [7,4,3,null,null,6,19], target = 3
17+
* Output: 3
18+
* Explanation: In all examples the original and cloned trees are shown. The target node is a green node from the original tree. The answer is the yellow node from the cloned tree.
19+
*
20+
* Example 2:
21+
* Input: tree = [7], target = 7
22+
* Output: 7
23+
*
24+
* Example 3:
25+
* Input: tree = [8,null,6,null,5,null,4,null,3,null,2,null,1], target = 4
26+
* Output: 4
27+
*
28+
* Example 4:
29+
* Input: tree = [1,2,3,4,5,6,7,8,9,10], target = 5
30+
* Output: 5
31+
*
32+
* Example 5:
33+
* Input: tree = [1,2,null,3], target = 2
34+
* Output: 2
35+
*
36+
* Constraints:
37+
* The number of nodes in the tree is in the range [1, 10^4].
38+
* The values of the nodes of the tree are unique.
39+
* target node is a node from the original tree and is not null.
40+
* */
41+
public class _1379 {
42+
public static class Solution1 {
43+
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
44+
if (original == null) {
45+
return null;
46+
}
47+
if (original.val == target.val) {
48+
return cloned;
49+
}
50+
TreeNode left = getTargetCopy(original.left, cloned.left, target);
51+
if (left != null && left.val == target.val) {
52+
return left;
53+
}
54+
return getTargetCopy(original.right, cloned.right, target);
55+
}
56+
}
57+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1379;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
public class _1379Test {
12+
private static _1379.Solution1 solution1;
13+
private static TreeNode original;
14+
private static TreeNode cloned;
15+
private static TreeNode target;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _1379.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
original = TreeUtils.constructBinaryTree(Arrays.asList(7, 4, 3, null, null, 6, 19));
25+
cloned = TreeUtils.constructBinaryTree(Arrays.asList(7, 4, 3, null, null, 6, 19));
26+
target = TreeUtils.constructBinaryTree(Arrays.asList(3, 6, 19));
27+
TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target));
28+
}
29+
30+
@Test
31+
public void test2() {
32+
original = TreeUtils.constructBinaryTree(Arrays.asList(7));
33+
cloned = TreeUtils.constructBinaryTree(Arrays.asList(7));
34+
target = TreeUtils.constructBinaryTree(Arrays.asList(7));
35+
TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target));
36+
}
37+
38+
@Test
39+
public void test3() {
40+
original = TreeUtils.constructBinaryTree(Arrays.asList(8, null, 6, null, 5, null, 4, null, 3, null, 2, null, 1));
41+
cloned = TreeUtils.constructBinaryTree(Arrays.asList(8, null, 6, null, 5, null, 4, null, 3, null, 2, null, 1));
42+
target = TreeUtils.constructBinaryTree(Arrays.asList(4, null, 3, null, 2, null, 1));
43+
TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target));
44+
}
45+
46+
@Test
47+
public void test4() {
48+
original = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
49+
cloned = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
50+
target = TreeUtils.constructBinaryTree(Arrays.asList(5, 10));
51+
TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target));
52+
}
53+
54+
@Test
55+
public void test5() {
56+
original = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 3));
57+
cloned = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, null, 3));
58+
target = TreeUtils.constructBinaryTree(Arrays.asList(2, 3));
59+
TreeUtils.printBinaryTree(solution1.getTargetCopy(original, cloned, target));
60+
}
61+
62+
}

0 commit comments

Comments
 (0)