[抄题]:

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

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

以为用直接用helper函数就可以:dfs(root.left, k - root.val) || dfs(root.right, k - root.val);

但是这样做的问题是一定会把当前的root.val包括进去,然而结果其实可以不包括当前root节点的值

[一句话思路]:

任意取点时,把之前出现过的点先用hashset缓存一下。头一次见。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

任意取点时,把之前出现过的点先用hashset缓存一下。头一次见。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

哈希集合缓存

[关键模板化代码]:

DFS,但是要缓存

if(root == null)return false;
//exit: set contains k - root.val;
if (set.contains(k - root.val)) return true;
//store the root.val at present
set.add(root.val);
//expand to left, right
return dfs(root.left, set, k) || dfs(root.right, set, k);

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean findTarget(TreeNode root, int k) {
HashSet<Integer> set = new HashSet<>();
return dfs(root, set, k);
} public boolean dfs(TreeNode root, HashSet<Integer> set, int k){
if(root == null)return false;
//exit: set contains k - root.val;
if (set.contains(k - root.val)) return true;
//store the root.val at present
set.add(root.val);
//expand to left, right
return dfs(root.left, set, k) || dfs(root.right, set, k);
}
}

653. Two Sum IV - Input is a BST 二叉树版本的更多相关文章

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. centos下svn的主要常用命令(解决商城系统添加的文件无法自动更新至svn服务器)

    问题描述: 在商城中通过网页上传的png文件无法自动添加到版本库中. 查找过程: 通过程序分析,增加的主要是数据文件,主要分布在data目录中. svn list /home/ggg --depth= ...

  2. 5.Python使用最新爬虫工具requests-html

    1.安装,在命令行输入:pip install requests-html,安装成功后,在Pycharm引入即可. 2.代码如下所示: from requests_html import HTMLSe ...

  3. 运维命令:tcpdump

    tcpdump命令  tcpdump 命令是一款sniffer工具,它可以打印所有经过网络接口的数据包的头信息,也可以使用 -w 选项将数据包保存到文件中,方便以后分析. 常用参数: -a:尝试将网络 ...

  4. mysql存储引擎之innodb学习

    innodb引擎特点1.支持事务:支持4个事务隔离级别,支持多版本读. 2.行级锁定(更新时一般是锁定当前行):通过索引实现,全表扫描仍然会是表锁,注意间隙 锁的影响 3.读写阻塞与事务隔离级别有关 ...

  5. 免费数据集下载网站【dataset】

    https://github.com/awesomedata/awesome-public-datasets

  6. django 网站 Hello world

    环境搭建 1.python2.7,python3.x均可以使用, 2.直接pip install django或者去下载whl文件安装 3.用eclipse和pycharm均可 开始 1.进入一个目录 ...

  7. FPGA时序约束一点总结

    时序约束的一点总结. 打拍.掌握好时序. 手动分配位置,这个不是一定有效. 打破层级或者物理综合,或者自动加流水等综合优化参数调整. 根据实际情况使用异步时钟策略. 换速度更快的片子. 最也进接手一个 ...

  8. 页面初始加载的是默认刷新一次(f5)

    参考找不到了,不好意思.. 两种可以都试一下,解决问题就好了. 1.----------- <script type="text/javascript"> window ...

  9. java代码------计算器

    总结:我用if()语句写计算功能的代码时,实现不了,与switch_-catch语句不一样.不知到怎么实现 package com.p; import javax.swing.*; import ja ...

  10. Code blocks调试教程

    特别声明:本教程已转移至百度经验:https://jingyan.baidu.com/article/6b182309939a87ba58e159bf.html 一.题外话 之前一直想用Code bl ...