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.   
  two sum 很经典,经典的题目是对一个数组进行two sum或者 three sum,排序后利用双指针检索,可以压缩搜索空间。
  类似的还有以下几道题:
 
  解题思路:

  1、递归去做,用hash_set 存储树中的树,然后进行查询是否当前遍历的结点能和之前中的任意一个树加和凑成k
/**
* 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:
bool findTarget(TreeNode* root, int k) {
//用一个vector存起来 然后相加 但是这样没用到搜索树的性质 中序遍历就是一个有序的表
//用hash_set 来从所有的数值 以及是否当前做差能找到这个数
unordered_set<int> res;
return helper(root,res,k);
}
bool helper(TreeNode* node,unordered_set<int> &res,int k )
{
if(node==NULL) return false; //空了就返回false
if(res.count(k-node->val)) return true;//找到了就终止递归
//存储当前结点的数值
res.insert(node->val);
return helper(node->left, res,k) ||helper(node->right,res,k); }
};

【leetcode】653. Two Sum IV - Input is a BST的更多相关文章

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

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

  2. 【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; 完

  3. 【easy】653. Two Sum IV - Input is a BST

    BST树求得两个节点的和是target //因为是BST所以中序遍历得到的是递增数组 //这个题的方法就是在一个递增数组中找到两个数的和相加是目标结果 /** * Definition for a b ...

  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】167. Two Sum II - Input array is sorted

    Difficulty:easy  More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...

  6. [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 ...

  7. LeetCode算法题-Two Sum IV - Input is a BST(Java实现)

    这是悦乐书的第280次更新,第296篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第148题(顺位题号是653).给定二进制搜索树和目标数,如果BST中存在两个元素,使得 ...

  8. 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)

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

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

随机推荐

  1. 恶意代码の奇客pdf分析

    目录 恶意代码の奇客pdf分析 奇客PDF安装包分析 静态分析基础技术 上传杀毒网 查壳 编译时间 导入表分析 资源查看 动态分析基础技术 Process Monitor监控 Process Expl ...

  2. Java项目中集成钉钉机器人推送消息提醒

    前言: 项目中有一个需求,当有新订单产生的时候,希望能够及时通知到业务相关人员进行处理,整体考虑了一下,选用了钉钉机器人提醒功能(公司内部主要也是使用钉钉进行通讯). 操作: 主要分为两部分进行处理: ...

  3. Downward API —— 在容器内部获取 Pod 信息

    我们知道,每个 Pod 在被超过创建出来之后,都会被系统分配唯一的名字.IP地址,并且处于某个 Namespace 中,那么我们如何在 Pod 的容器内获取 Pod 的这些重要信息呢? 答案就是使用 ...

  4. Typora软件的使用教程

    一.Typora软件介绍 Typora是一款轻便简洁的Markdown编辑器,支持即时渲染技术,这也是与其他Markdown编辑器最显著的区别.即时渲染使得你写Markdown就想是写Word文档一样 ...

  5. springboot使用之请求参数与基本注解

    @PathVariable 作用:@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值,将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariabl ...

  6. initNativeTransServiceId . ntrans:object componentId :-368613127 微信小程序

    二维码打开的页面是否存在 注意:体验版二维码默认路径是 pages/index/index 我的因为分包的原因调整了首页路径 所以路径是pages/tabBar/search/search 如果不是这 ...

  7. uni-app map组件关于marker标记点动态设置的问题

    marker是Array类型,赋值的时候只能对整个数组进行更改赋值,不能只改变内部的对象,亲测Vue.$set()也不行 this.marker = [ { latitude: 39.90, long ...

  8. 【IDEA】IDEA项目没有被SVN管理问题

    解决方法 VCS-Enable Version Control Integration

  9. [第二章]c++学习笔记5(类型转换构造函数)

    使用例 析构函数 使用例 析构函数和数组 delete运算符导致析构函数的调用

  10. 菜鸡的Java笔记 第十七 static 关键字

    static 是java中定义的一个关键字,主要是描述全局的概念,所以利用static关键字可以定义属性,定义方法        但是在90%的情况下,我们的开发代码很少会去直接编写static*// ...