[抄题]:

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. Ubuntu 16.04 配置安卓5.1编译环境

    Ubuntu 16.04版本 电脑cpu必须是64位 硬盘分配大约100G的空间 1.ubuntu中更新源 $ sudo apt-get update 2.安装 openjdk-8-jdk $ sud ...

  2. python 异常处理,约束

    异常处理: 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是显示一个提示的页面. try: pass except Exception,ex: pass 例如: whi ...

  3. postman 请求 页面出现 Could not get any response 解决方法

    1.关闭掉证书 2.再关闭掉代理 3.去掉请求头的Content-Length字段 (或者一个个试请求头的值,看去掉那些就请求成功了) 后记: 网友 @ 重庆张晓祥 提供了个线索确实让我想起以前我从浏 ...

  4. MongoDB day04

    文件存储 文件存储到数据库的方式 1. 存储路径 将文件在本地的路径以字符串形式存储到数据库 优点 : 节省数据库空间 缺点 : 当数据库或者文件位置发生变化时文件丢失. 2. 存储文件本身 以二进制 ...

  5. Win10 恢复后退键

    转自:http://www.cnblogs.com/liubaicai/p/4368261.html 自带的几种风格的页面,竟然是用的左上角虚拟后退键,这种倒行逆施的行为微软你真是够了! 一定要把后退 ...

  6. win系统主机上的虚拟机NAT模式可修改3389端口做远程登录

    当你只有一个公网IP的时候,那么这个IP基本上是要分给物理主机的,那么本机的虚拟机就只能选NAT网络模式了,那么想在外网直接远程虚拟机的话,修改虚拟机的远程登录端口(不修改也可以,主要是主机的3389 ...

  7. FTP服务器的搭建与配置

    主要来源:http://www.cnblogs.com/helonghl/articles/5533857.html 1.安装FTP服务器: yum install vsftpd -y 2.启动FTP ...

  8. 第二章:Android Studio概述(一)[学习Android Studio汉化教程]

     Android Studio是一个视窗化的开发环境.为了充分利用有限的屏幕空间,不让你束手束脚,Android Studio 在特定的时间仅仅显示一小部分可用窗口. 除了一些上下文敏感的窗口和上下文 ...

  9. 微信登录失败,redirect_uri域名与后台配置不一致,错误代码10003

    微信登录失败,redirect_uri域名与后台配置不一致,错误代码10003 1 先检查网页的授权域名  不要带http:// 2 检查下自己的appid是否正确 我换了appid没上传,多花了时间 ...

  10. 一行命令解决 xcode升级新版本插件失效问题

    sudo find ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins -name Info.plist -maxdepth ...