forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSumIV.java
More file actions
26 lines (23 loc) · 691 Bytes
/
TwoSumIV.java
File metadata and controls
26 lines (23 loc) · 691 Bytes
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
import java.util.ArrayList;
import java.util.List;
/**
* https://leetcode.com/articles/two-sum-iv/
*/
public class TwoSumIV {
public boolean findTarget(TreeNode root, int k) {
List<Integer> nums = new ArrayList<>();
inorder(root, nums);
for (int i = 0, j = nums.size() - 1; i < j; ) {
if (nums.get(i) + nums.get(j) == k) return true;
if (nums.get(i) + nums.get(j) < k) i++;
else j--;
}
return false;
}
public void inorder(TreeNode root, List<Integer> nums) {
if (root == null) return;
inorder(root.left, nums);
nums.add(root.val);
inorder(root.right, nums);
}
}