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

Commit040066f

Browse files
add 1214
1 parent290d0dc commit040066f

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ _If you like this project, please leave me a star._ ★
5656
|1228|[Missing Number In Arithmetic Progression](https://leetcode.com/problems/missing-number-in-arithmetic-progression/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1228.java)||Easy||
5757
|1221|[Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1221.java)||Easy|Greedy|
5858
|1217|[Play with Chips](https://leetcode.com/problems/play-with-chips/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1217.java)||Easy|Array, Math, Greedy|
59+
|1214|[Two Sum BSTs](https://leetcode.com/problems/two-sum-bsts/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1214.java)||Medium| Binary Search Tree|
5960
|1213|[Intersection of Three Sorted Arrays](https://leetcode.com/problems/intersection-of-three-sorted-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1213.java)|[:tv:](https://www.youtube.com/watch?v=zceoOrHSHNQ)|Easy||
6061
|1207|[Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1207.java)|[:tv:](https://www.youtube.com/watch?v=_NYimlZY1PE)|Easy||
6162
|1200|[Minimum Absolute Difference](https://leetcode.com/problems/minimum-absolute-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1200.java)|[:tv:](https://www.youtube.com/watch?v=mH1aEjOEjcQ)|Easy||
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
5+
importjava.util.ArrayList;
6+
importjava.util.List;
7+
8+
/**
9+
* 1214. Two Sum BSTs
10+
*
11+
* Given two binary search trees,
12+
* return True if and only if there is a node in the first tree and a node in the second tree whose values sum up to a given integer target.
13+
*
14+
* Example 1:
15+
* 2 1
16+
* / \ / \
17+
* 1 4 0 3
18+
*
19+
* Input: root1 = [2,1,4], root2 = [1,0,3], target = 5
20+
* Output: true
21+
* Explanation: 2 and 3 sum up to 5.
22+
*
23+
* Example 2:
24+
* 0 5
25+
* / \ / \
26+
* -10 10 1 7
27+
* / \
28+
* 0 2
29+
*
30+
* Input: root1 = [0,-10,10], root2 = [5,1,7,0,2], target = 18
31+
* Output: false
32+
*
33+
* Constraints:
34+
* Each tree has at most 5000 nodes.
35+
* -10^9 <= target, node.val <= 10^9
36+
* */
37+
publicclass_1214 {
38+
publicstaticclassSolution1 {
39+
publicbooleantwoSumBSTs(TreeNoderoot1,TreeNoderoot2,inttarget) {
40+
if (root1 ==null ||root2 ==null) {
41+
returnfalse;
42+
}
43+
List<Integer>inorder1 =inorderDfs(root1,newArrayList<>());
44+
List<Integer>inorder2 =inorderDfs(root2,newArrayList<>());
45+
returnfindTwoSum(inorder1,inorder2,target);
46+
}
47+
48+
privatebooleanfindTwoSum(List<Integer>sorted1,List<Integer>sorted2,inttarget) {
49+
for (inti =0;i <sorted1.size();i++) {
50+
if (exists(sorted2,target -sorted1.get(i))) {
51+
returntrue;
52+
}
53+
}
54+
returnfalse;
55+
}
56+
57+
privatebooleanexists(List<Integer>sorted,inttarget) {
58+
intleft =0;
59+
intright =sorted.size() -1;
60+
while (left <=right) {
61+
intmid =left + (right -left) /2;
62+
if (sorted.get(mid) ==target) {
63+
returntrue;
64+
}elseif (sorted.get(mid) <target) {
65+
left =mid +1;
66+
}else {
67+
right =mid -1;
68+
}
69+
}
70+
returnfalse;
71+
}
72+
73+
privateList<Integer>inorderDfs(TreeNoderoot,List<Integer>list) {
74+
if (root ==null) {
75+
returnlist;
76+
}
77+
if (root.left !=null) {
78+
list =inorderDfs(root.left,list);
79+
}
80+
list.add(root.val);
81+
if (root.right !=null) {
82+
list =inorderDfs(root.right,list);
83+
}
84+
returnlist;
85+
}
86+
87+
}
88+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.common.classes.TreeNode;
4+
importcom.fishercoder.common.utils.TreeUtils;
5+
importcom.fishercoder.solutions._1214;
6+
importorg.junit.BeforeClass;
7+
importorg.junit.Test;
8+
9+
importjava.util.Arrays;
10+
11+
importstaticorg.junit.Assert.assertEquals;
12+
13+
publicclass_1214Test {
14+
privatestatic_1214.Solution1solution1;
15+
privatestaticTreeNoderoot1;
16+
privatestaticTreeNoderoot2;
17+
18+
@BeforeClass
19+
publicstaticvoidsetup() {
20+
solution1 =new_1214.Solution1();
21+
}
22+
23+
@Test
24+
publicvoidtest1() {
25+
root1 =TreeUtils.constructBinaryTree(Arrays.asList(2,1,4));
26+
root2 =TreeUtils.constructBinaryTree(Arrays.asList(1,0,3));
27+
assertEquals(true,solution1.twoSumBSTs(root1,root2,5));
28+
}
29+
30+
@Test
31+
publicvoidtest2() {
32+
root1 =TreeUtils.constructBinaryTree(Arrays.asList(0, -10,10));
33+
root2 =TreeUtils.constructBinaryTree(Arrays.asList(5,1,7,0,2));
34+
assertEquals(false,solution1.twoSumBSTs(root1,root2,18));
35+
}
36+
37+
@Test
38+
publicvoidtest3() {
39+
root1 =TreeUtils.constructBinaryTree(Arrays.asList(0, -10,10));
40+
root2 =TreeUtils.constructBinaryTree(Arrays.asList(5,1,7,0,2));
41+
assertEquals(true,solution1.twoSumBSTs(root1,root2,17));
42+
}
43+
44+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp