forked from havanagrawal/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_653.java
101 lines (88 loc) · 2.5 KB
/
_653.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
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.fishercoder.solutions;
import com.fishercoder.common.classes.TreeNode;
import com.fishercoder.common.utils.Notes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 653. Two Sum IV - Input is a BST
*
* Given a Binary Search Tree and a target number,
* return true if there exist two elements in the BST such that their sum is equal to the given target.
Example 1:
Input:
5
/ \
3 6
/ \ \
2 4 7
Target = 9
Output: True
Example 2:
Input:
5
/ \
3 6
/ \ \
2 4 7
Target = 28
Output: False
*/
public class _653 {
public static class ListSolution {
public boolean findTarget(TreeNode root, int k) {
if (root == null) {
return false;
}
List<Integer> list = new ArrayList<>();
dfs(root, list);
for (int i = 0; i < list.size() - 1; i++) {
for (int j = i + 1; j < list.size(); j++) {
if (list.get(i) + list.get(j) == k) {
return true;
}
}
}
return false;
}
private void dfs(TreeNode root, List<Integer> list) {
list.add(root.val);
if (root.left != null) {
dfs(root.left, list);
}
if (root.right != null) {
dfs(root.right, list);
}
}
}
@Notes(todo = "This solution fails by _653Test.test6(), need to fix it.")
public static class MapSolution {
public boolean findTarget(TreeNode root, int k) {
if (root == null) {
return false;
}
Map<Integer, Integer> map = new HashMap();//value is index
int index = 0;
preorder(root, map, index);
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (map.containsKey(k - entry.getKey()) && map.get(k - entry.getKey()) != entry.getValue()) {
return true;
}
}
return false;
}
private void preorder(TreeNode root, Map<Integer, Integer> map, int index) {
if (root == null) {
return;
}
map.put(root.val, index++);
if (root.left != null) {
preorder(root.left, map, ++index);
}
if (root.right != null) {
preorder(root.right, map, ++index);
}
}
}
}