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
/**
* 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:
void dfs(TreeNode* root){
if(root==NULL) return;
nums.push_back(root->val);
checked[root->val]++;
dfs(root->left);
dfs(root->right);
}
bool findTarget(TreeNode* root, int k) {
dfs(root);
for(int i=0;i<nums.size();i++)
if(k-nums[i]!=nums[i]&&checked[k-nums[i]]>=1)
return true;
else if(k-nums[i]==nums[i]&&checked[nums[i]]>=2)
return true;
return false;
}
private:
vector<int> nums;
map<int,int> checked;
};

LeetCode 653. Two Sum IV – Input is a BST的更多相关文章

  1. [LeetCode] 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 s ...

  2. LeetCode - 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 s ...

  3. LeetCode 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 ...

  4. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  5. 【Leetcode_easy】653. Two Sum IV - Input is a BST

    problem 653. Two Sum IV - Input is a BST 参考 1. Leetcode_easy_653. Two Sum IV - Input is a BST; 完

  6. 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...

  7. [LeetCode&Python] Problem 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 s ...

  8. 【leetcode】653. Two Sum IV - Input is a BST

    Given the root of a Binary Search Tree and a target number k, return true if there exist two element ...

  9. 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 s ...

随机推荐

  1. 洛谷 P1169||bzoj1057 [ZJOI2007]棋盘制作

    洛谷P1169 bzoj1057 这个题目跟最大全0子矩阵是类似的.正方形的话,只要把任意极大子正方形(”极大“定义见后面的”论文“)当成把某个极大子矩形去掉一块变成正方形即可,容易解决. 解法1:看 ...

  2. js实现屏幕自适应局部

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  3. I/O————File对象

    File文件对象 文件和文件夹都是用File代表 创建一个文件对象,(并不会有真正的文件或文件夹被创建) File f1 = new File("d:/lolfilder"); S ...

  4. [问题解决]Deepin环境变量设置无效解决

    将环境变量的设置放在这里:  gedit ~/.bashrc 详见参考: linux下如何设置环境变量PATH : http://blog.csdn.net/witsmakemen/article/d ...

  5. ios,弹窗遮罩滚动穿透解决方案

  6. jQuery对表格的操作

    1.表格变色 (1)普通的隔行变色 CSS代码: .even{background:#fff;}   //偶数行样式 .even{background:#fff;}   //奇数行样式 ①包括表头 $ ...

  7. jquery中ajax请求后台数据成功后既不执行success也不执行error解决方法

    jquery中ajax请求后台数据成功后既不执行success也不执行error,此外系统报错:Uncaught SyntaxError: Unexpected identifier at Objec ...

  8. Ubuntu下HTTPS配置

    Ubuntu下HTTPS配置非常简单,对大部分用户而言,使用普通的自签名证书,只需按照步骤进行就可以了,无需了解密钥.证书的更多知识,更深的背景知识还有RSA算法.DES算法.X509规范.CA机构. ...

  9. codevs 3096 流输入练习——寻找Sb.VI

    时间限制: 1 s  空间限制: 16000 KB  题目等级 : 白银 Silver 题目描述 Description 已知某开放授权人员名叫Serb,由于经常修改各种数据,因此开发人员们都喊他SB ...

  10. Mac app 破解之路

    6年之前一直做过一些内存挂,脚本挂.都是比较低级的技术. 这几年期间,断断续续利用业余时间学了一些汇编的知识,当时只是想着破解游戏. 所有的黑技术都是业余自学的,没有老师可以问,只能百度和自己领悟,比 ...