653. Two Sum IV - Input is a BST

#### QUESTION:

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

#### EXPLANATION:

1.遍历这个二叉树

2.每一个数和二叉树中的节点进行对撞

3.如果对撞成功就返回true

#### SOLUTION:

class Solution {
public boolean findTarget(TreeNode root, int k) {
Queue<TreeNode> queue = new ArrayDeque<>();
while (!queue.isEmpty()){
TreeNode tmp = queue.poll();
boolean find = false;
find = findTargetHelper(root,k,tmp.val);
if(find) return true;
}
return false;
}

public boolean findTargetHelper(TreeNode root,int k,int val){
if(root == null) return false;
if(root.val + val == k && root.val != val ) return true;
if(root.val < k - val) return findTargetHelper(root.right,k,val);
if(root.val > k - val) return findTargetHelper(root.left,k,val);
return false;
}

}
>