#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.
这道题的意思是,给一个BST和一个目标数,问这个BST中有没有两个节点的值的和是这个目标数。
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @param {number} k
* @return {boolean}
*/
var findTarget = function(root, k, nodes = []) {
if(root !== null) {
if(nodes.includes(k-root.val)) {
return true;
}
else {
nodes.push(root.val);
}
if(findTarget(root.left, k ,nodes) || findTarget(root.right, k ,nodes)) {
return true;
}
}
return false;
};