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:

  1. Input:
  2. 5
  3. / \
  4. 3 6
  5. / \ \
  6. 2 4 7
  7. Target = 9
  8. Output: True

Example 2:

  1. Input:
  2. 5
  3. / \
  4. 3 6
  5. / \ \
  6. 2 4 7
  7. Target = 28
  8. Output: False
  1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. void dfs(TreeNode* root){
  13. if(root==NULL) return;
  14. nums.push_back(root->val);
  15. checked[root->val]++;
  16. dfs(root->left);
  17. dfs(root->right);
  18. }
  19. bool findTarget(TreeNode* root, int k) {
  20. dfs(root);
  21. for(int i=0;i<nums.size();i++)
  22. if(k-nums[i]!=nums[i]&&checked[k-nums[i]]>=1)
  23. return true;
  24. else if(k-nums[i]==nums[i]&&checked[nums[i]]>=2)
  25. return true;
  26. return false;
  27. }
  28. private:
  29. vector<int> nums;
  30. map<int,int> checked;
  31. };

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. 使用express+mongoDB搭建多人博客 学习(3)connect-flash和mongodb,表单注册

    1.根目录下新建settings.js,存放数据库配置 module.exports={ cookieSecret:"myblog", db:"blog", h ...

  2. 禁用和关闭ECSHOP缓存

    ECSHOP的缓存机制从一定程度上可以减少ECSHOP反复读取数据库的几率,从而一定程度上降低服务器负担,提高访问速度. 但是启用缓存机制,对一些新手站长也有不利的地方.我就遇到很多新手站长经常问,我 ...

  3. BS3 多级菜单

    <div class="container"> <div class="row"> <h2>Multi level drop ...

  4. MVC系列学习(十六)-区域的学习

    1.查找控制器的过程 1.1调用其他项目中的控制器 a.先到网站根目录下的bin文件夹下,遍历所有的程序集 b.找到以Controller结尾的类 c.再找出其中继承了Controller的类 d.接 ...

  5. 重新安装Magento CE 2.1.0

    删除 var/cache 文件夹 删除 var/generation文件夹 删除 app/etc/config.php 删除 app/etc/env.php 如果您觉得阅读本文对您有帮助,欢迎转载本文 ...

  6. cvCanny的参数

    cvCanny 函数功能:采用Canny方法对图像进行边缘检测 函数原型: void cvCanny( const CvArr* image, CvArr* edges, double thresho ...

  7. https域名强弱校验的区别

    HttpsURLConnection.setDefaultHostnameVerifier( new HostnameVerifier(){ public boolean verify(String ...

  8. Spark中Java函数的使用方法笔记

    1: map 函数map是对RDD中的每个元素都执行一个指定的函数来产生一个新的RDD. 任何原RDD中的元素在新RDD中都有且只有一个元素与之对应. 2: mapPartitions函数</p ...

  9. JBOSS默认连接池配置

    jboss5.0mysql连接配置 <?xml version="1.0" encoding="UTF-8"?> <!-- The Hyper ...

  10. Ubuntu下Postgres安装与配置

    postgres8.4安装配置:1.安装postgres8.4~$ sudo apt-get install postgresql 2.修改超级管理员postgres密码:以系统用户运行psql~$ ...