Skip to content

Commit 9bc8e4f

Browse files
add 1305
1 parent 66611fb commit 9bc8e4f

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-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+
|1305|[All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1305.java) | |Medium||
1112
|1304|[Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1304.java) | |Easy||
1213
|1302|[Deepest Leaves Sum](https://leetcode.com/problems/deepest-leaves-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1302.java) | |Medium||
1314
|1300|[Sum of Mutated Array Closest to Target](https://leetcode.com/problems/sum-of-mutated-array-closest-to-target/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1300.java) | |Medium||
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
/**
10+
* 1305. All Elements in Two Binary Search Trees
11+
*
12+
* Given two binary search trees root1 and root2.
13+
* Return a list containing all the integers from both trees sorted in ascending order.
14+
*
15+
* Example 1:
16+
* Input: root1 = [2,1,4], root2 = [1,0,3]
17+
* Output: [0,1,1,2,3,4]
18+
*
19+
* Example 2:
20+
* Input: root1 = [0,-10,10], root2 = [5,1,7,0,2]
21+
* Output: [-10,0,0,1,2,5,7,10]
22+
*
23+
* Example 3:
24+
* Input: root1 = [], root2 = [5,1,7,0,2]
25+
* Output: [0,1,2,5,7]
26+
*
27+
* Example 4:
28+
* Input: root1 = [0,-10,10], root2 = []
29+
* Output: [-10,0,10]
30+
*
31+
* Example 5:
32+
* Input: root1 = [1,null,8], root2 = [8,1]
33+
* Output: [1,1,8,8]
34+
*
35+
* Constraints:
36+
* Each tree has at most 5000 nodes.
37+
* Each node's value is between [-10^5, 10^5].*/
38+
public class _1305 {
39+
public static class Solution1 {
40+
public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
41+
List<Integer> list1 = getAllNodes(root1);
42+
List<Integer> list2 = getAllNodes(root2);
43+
List<Integer> merged = new ArrayList<>();
44+
merged.addAll(list1);
45+
merged.addAll(list2);
46+
Collections.sort(merged);
47+
return merged;
48+
}
49+
50+
private List<Integer> getAllNodes(TreeNode root) {
51+
List<Integer> list = new ArrayList<>();
52+
return inorder(root, list);
53+
}
54+
55+
List<Integer> inorder(TreeNode root, List<Integer> result) {
56+
if (root == null) {
57+
return result;
58+
}
59+
inorder(root.left, result);
60+
result.add(root.val);
61+
return inorder(root.right, result);
62+
}
63+
}
64+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1305;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class _1305Test {
14+
private static _1305.Solution1 solution1;
15+
private static TreeNode root1;
16+
private static TreeNode root2;
17+
18+
@BeforeClass
19+
public static void setup() {
20+
solution1 = new _1305.Solution1();
21+
}
22+
23+
@Test
24+
public void test1() {
25+
root1 = TreeUtils.constructBinaryTree(Arrays.asList(2, 1, 4));
26+
root2 = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 3));
27+
assertEquals(Arrays.asList(0, 1, 1, 2, 3, 4), solution1.getAllElements(root1, root2));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)