forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1214.java
88 lines (81 loc) · 2.58 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* 1214. Two Sum BSTs
*
* Given two binary search trees,
* 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.
*
* Example 1:
* 2 1
* / \ / \
* 1 4 0 3
*
* Input: root1 = [2,1,4], root2 = [1,0,3], target = 5
* Output: true
* Explanation: 2 and 3 sum up to 5.
*
* Example 2:
* 0 5
* / \ / \
* -10 10 1 7
* / \
* 0 2
*
* Input: root1 = [0,-10,10], root2 = [5,1,7,0,2], target = 18
* Output: false
*
* Constraints:
* Each tree has at most 5000 nodes.
* -10^9 <= target, node.val <= 10^9
* */
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;
}
}
}