Skip to content

Latest commit

 

History

History
102 lines (80 loc) · 2.05 KB

653-two-sum-iv-input-is-a-bst.md

File metadata and controls

102 lines (80 loc) · 2.05 KB

653. Two Sum IV - Input is a BST - 两数之和 IV - 输入 BST

给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。

案例 1:

输入: 
    5
   / \
  3   6
 / \   \
2   4   7

Target = 9

输出: True

 

案例 2:

输入: 
    5
   / \
  3   6
 / \   \
2   4   7

Target = 28

输出: False

 


题目标签:Tree

题目链接:LeetCode / LeetCode中国

题解

Language Runtime Memory
cpp 36 ms N/A
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool findTarget(TreeNode* root, int k) {
        if(!root){
            return false;
        }
        queue<TreeNode*> seq;
        set<TreeNode*> vt;
        multiset<int> vals;
        vt.insert(root);
        seq.push(root);
        while(!seq.empty()){
            TreeNode* tmp = seq.front();
            seq.pop();
            if(tmp->left && !vt.count(tmp->left)){
                vt.insert(tmp->left);
                seq.push(tmp->left);
            }
            if(tmp->right && !vt.count(tmp->right)){
                vt.insert(tmp->right);
                seq.push(tmp->right);
            }
        }
        for(TreeNode* node : vt){
            vals.insert(node->val);
        }
        for(int val : vals){
            int target = k - val;
            if(vals.count(target) > (int)(target == val)){
                return true;
            }
        }
        return false;
    }
};