Problem

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Input: 
5
/ \
3 6
/ \ \
2 4 7

Target = 9

Output: True
---------------------
Input:
5
/ \
3 6
/ \ \
2 4 7

Target = 28

Output: False

Thinking

先将BST中的value存放到list中,再用双指针


Answer

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
public class Solution {

public boolean findTarget(TreeNode root, int k) {
List<Integer> collector = new ArrayList<>();
save(collector, root);
return isTwoSum(collector, k);
}

// 将BST的值存到List中
public void save(List<Integer> collector, TreeNode x){
if (x == null)
return;
save(collector, x.left);
collector.add(x.val);
save(collector, x.right);
}

public boolean isTwoSum(List<Integer> collector, int k){
int low = 0, high = collector.size()-1;
while (low < high){
int t = collector.get(low) + (int)collector.get(high);
if (t < k) low++;
else if (t > k) high--;
else return true;
}
return false;
}

}