-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1214.java
More file actions
78 lines (70 loc) · 2.05 KB
/
LeetCode1214.java
File metadata and controls
78 lines (70 loc) · 2.05 KB
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
package codeTop;
import java.util.HashSet;
/**
* Given the roots of two binary tree,root1 and root2,return true if and only if there is a node in the first tree
* and a node in the second tree whose value sum up to a given integer target
*/
public class LeetCode1214 {
HashSet<Integer> set = new HashSet<>();
/**
* 时间负责度:O(N),空间复杂度:O(n)
* @param root1
* @param root2
* @param target
* @return
*/
public boolean twoSumBSTS(TreeNode root1,TreeNode root2,Integer target){
if(root1 == null || root2 == null){
return false;
}
dfs(root1);
return process(root2,target);
}
//将TreeNode root的所有节点都放到set中
private void dfs(TreeNode root){
if(root != null){
set.add(root.value);
dfs(root.left);
dfs(root.right);
}
}
private boolean process(TreeNode root,Integer target){
if(root == null){
return false;
}
int diff = target - root.value;
if(set.contains(diff)){
return true;
}
return process(root.left,target) || process(root.right,target);
}
/**
* 方法二:O(nlogn),固定其中一个bst,二分查找另一个。
*
*/
public boolean twoSumBSTS2(TreeNode root1,TreeNode root2,Integer target){
if(root1 == null) return false;
if(find(root2,target - root1.value)) return true;
return twoSumBSTS2(root1.left,root2,target) || twoSumBSTS2(root1.right,root2,target);
}
private boolean find(TreeNode root,Integer target){
if(root == null){
return false;
}
if(root.value == target){
return true;
}else if(root.value > target){
return find(root.left,target);
}else {
return find(root.right,target);
}
}
}
class TreeNode{
public Integer value;
public TreeNode left;
public TreeNode right;
public TreeNode(Integer value){
this.value = value;
}
}