← All problems

653. Two Sum IV Input Is a BST

EasyOpen on LeetCodeProblem statement

Problem Statement

653. Two Sum IV - Input is a BST

Easy


Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target.

 

Example 1:

Input: root = [5,3,6,2,4,null,7], k = 9
Output: true

Example 2:

Input: root = [5,3,6,2,4,null,7], k = 28
Output: false

 

Constraints:

C++

Source file
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    unordered_map<int, int> m;
    bool findTarget(TreeNode* root, int k) {
        if(!root)
            return false;
        if(m[k-root->val])
            return true;
        m[root->val]++;
        return (findTarget(root->left, k) || findTarget(root->right, k));
    }
};