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. MyBatis中时间格式的映射问题

    简单地说,就是Java的Date类可以直接映射到Mysql的TIMESTAMP或者是DATETIME(按道理应该是映射成DATE的) 具体的看这两篇博客吧: 1. MySql中TIMESTAMP和DA ...

  2. [已读]精通AngularJS

    觉得可以看第二遍,内容其实还不错啦,就是翻译会有点生硬.

  3. Nodejs 定时任务

    安装扩展:node-schedule npm install node-schedule 1.linux Crontab风格 var schedule = require('node-schedule ...

  4. c# 定时器 自动执行

    //下面讲一个打开窗体定时执行按钮的东西 private void Form1_Load(object sender, EventArgs e) { System.Timers.Timer pTime ...

  5. Java虚拟机(JVM),JDK,JRE和JVM的区别——通过示例学习Java编程(2)

    Java虚拟机(JVM),JDK,JRE和JVM的区别 作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid ...

  6. 向fedora添加rpmfusion源

    http://blog.csdn.net/pu1030/article/details/7332036 有的rpmfusion地址有版本问题,找到一个比较好用的摘录一下: 从http://downlo ...

  7. HTTP/1.1 持久连接 persistent connection

    首先:HTTP的长连接和短连接本质上是TCP长连接和短连接. 1. 在HTTP1.0中,默认的是短连接,没有正式规定 Connection:Keep-alive 操作:在HTTP1.1中所有连接都是K ...

  8. [Rational Rose 2007]解决启动报”解决无法启动此程序因为丢失suite objects.dll“的问题

    问题根源1:不是丢失suite objects.dll文件,而是环境变量配置错误或无配置 假如安装目录如:C:\Program Files\Rational 需要配置环境变量的路径为:C:\Progr ...

  9. asp.net 页面嵌套(非iframe)方法

    前台 <div id="divUrlDetail" runat="server"> </div> 后台 protected void P ...

  10. 通过StringBuilder的reverse()实现倒序

    import java.util.Scanner; public class ReverseString { public static void main(String[] args) { Scan ...