[抄题]:

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.  
  8. Target = 9
  9.  
  10. Output: True

Example 2:

  1. Input:
  2. 5
  3. / \
  4. 3 6
  5. / \ \
  6. 2 4 7
  7.  
  8. Target = 28
  9.  
  10. 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,但是要缓存

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

[其他解法]:

[Follow Up]:

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

[代码风格] :

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. public boolean findTarget(TreeNode root, int k) {
  12. HashSet<Integer> set = new HashSet<>();
  13. return dfs(root, set, k);
  14. }
  15.  
  16. public boolean dfs(TreeNode root, HashSet<Integer> set, int k){
  17. if(root == null)return false;
  18. //exit: set contains k - root.val;
  19. if (set.contains(k - root.val)) return true;
  20. //store the root.val at present
  21. set.add(root.val);
  22. //expand to left, right
  23. return dfs(root.left, set, k) || dfs(root.right, set, k);
  24. }
  25. }

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. bzoj 4815 [Cqoi2017]小Q的表格——反演+分块

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4815 大概就是推式子的时候注意有两个边界都是 n ,考虑变成 2*... 之类的. 分块维护 ...

  2. java的时间

    先看例子: import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; imp ...

  3. couchdb的使用例子

    couchdb安装 sudo apt-get install erlang sudo apt-get install libmozjs185-dev libicu-dev 下载源码,编译安装 启动以后 ...

  4. m'ybatis 一对一 一对多 配置详解

    javabean: package com.me.model; import java.io.Serializable; import java.util.Date; import java.util ...

  5. 皆在FPGA之外

    最近做电力方面的项目,由于跨行业,所以很长一段时间都在做前期准备工作. 项目设计前应尽量做到面面俱到,否则会在项目设计中遇到下面大概率问题: 性能不满足需求,然后为了提升性能,资源又成了瓶颈: 功能设 ...

  6. Hibernate学习9—检索策略

    本章,采用Class和Student     ——  1 对 多的关系: Student.java: package com.cy.model; public class Student { priv ...

  7. jmeter数据关联_后置处理器_正则表达式提取器

  8. mysql 优化(3)

    using filesort 不能利用索引来进行分组或排序,利用filesort算法在内存或者磁盘进行排序using temporary 先在内存中进行分组,归并等操作,不够利用磁盘 SELECT i ...

  9. python基础知识之列表、元祖、字典、集合、字符串。

    1.可变类型之列表 列表用 [ ]来定义是可变的,可以通过索引值来去查询里面的字段可以可以追加,删除等 names='zhangyang guyun xiangpeng xuliangwei' nam ...

  10. SpringBoot起步依赖和自动配置

    一.起步依赖 1. 是什么 本质上是一个Maven项目对象模型(Project Object Model, POM), 定义了对其他库的传递依赖,这些东西加在一起即支持某项功能. 比如: spring ...