forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_1214.java
59 lines (52 loc) · 1.83 KB
/
_1214.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class _1214 {
public static class Solution1 {
public boolean twoSumBSTs(TreeNode root1, TreeNode root2, int target) {
if (root1 == null || root2 == null) {
return false;
}
List<Integer> inorder1 = inorderDfs(root1, new ArrayList<>());
List<Integer> inorder2 = inorderDfs(root2, new ArrayList<>());
return findTwoSum(inorder1, inorder2, target);
}
private boolean findTwoSum(List<Integer> sorted1, List<Integer> sorted2, int target) {
for (int i = 0; i < sorted1.size(); i++) {
if (exists(sorted2, target - sorted1.get(i))) {
return true;
}
}
return false;
}
private boolean exists(List<Integer> sorted, int target) {
int left = 0;
int right = sorted.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (sorted.get(mid) == target) {
return true;
} else if (sorted.get(mid) < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return false;
}
private List<Integer> inorderDfs(TreeNode root, List<Integer> list) {
if (root == null) {
return list;
}
if (root.left != null) {
list = inorderDfs(root.left, list);
}
list.add(root.val);
if (root.right != null) {
list = inorderDfs(root.right, list);
}
return list;
}
}
}