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

只要是两数之和的题,一定要记得用哈希表来做,这道题只不过是把数组变成了一棵二叉树而已

/**
* 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) {
if(root == null){
return false;
}
Set<Integer> set = new HashSet<>();
Queue<TreeNode> queue =new LinkedList<>();
queue.add(root);
while(!queue.isEmpty()){
TreeNode node = queue.poll();
if(set.contains(node.val)){
return true;
}
set.add(k-node.val);
if(node.left != null){queue.add(node.left);}
if(node.right != null){queue.add(node.right);}
}
return false;
}
}

LeetCode - Two Sum IV - Input is a BST的更多相关文章

  1. [LeetCode] 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. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

    653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...

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

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

  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】653. Two Sum IV - Input is a BST 解题报告(Python)

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

  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算法题-Two Sum IV - Input is a BST(Java实现)

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

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

随机推荐

  1. sqlalchemy(一)常用连接参数及包

    简介: 本文默认你已经有了一定的数据库基础.我们不喜欢写原生SQL语句,那个写着费劲,日常开发时候,我们怎么CRUD数据库呢?一般使用ORM,对象关系映射(英语:Object Relational M ...

  2. List在遍历中删除t元素

    法一:使用普通for循环遍历 注意: 1.从头开始循环,每次删除后 i  减一.             2.从尾开始循环. public class Main { public static voi ...

  3. C/C++知识补充(2) C/C++操作符/运算符的优先级 & 结合性

    , 逗号操作符 for( i = 0, j = 0; i < 10; i++, j++ ) ... 从左到右   Precedence Operator Description Example ...

  4. 玩转X-CTR100 l STM32F4 l 定时器时间测量

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍X-CTR100控制器 使用处理器内部硬件定 ...

  5. Excel日常操作

    1.固定表头 视图+冻结窗口+选择 2.下拉列表 数据+数据验证+序列+来源 筛选值也可是函数,函数值区间可以选择,然后隐藏该列数据即可 使用函数: 如果需要函数的值其他列也使用类似函数则拖动同样格式 ...

  6. L253 Work and Pleasure

    To be really happy and really safe, one ought to have at least two or three hobbies, and they must a ...

  7. delphi reintroduce作用

    当在子类中重载或者重新声明父类的虚方法时,使用  reintroduce   关键字告知编译器,可以消除警告信息. 如: TParent = class procedure proc;virtual; ...

  8. UUID自动生成

    (uuid,available,createtime)  select left(replace(uuid(), '-', ''),24),1,utc_timestamp() 使用: ),,utc_t ...

  9. git 服务器安装流程

    参考:https://git-scm.com/book/zh/v2/%E6%9C%8D%E5%8A%A1%E5%99%A8%E4%B8%8A%E7%9A%84-Git-%E5%9C%A8%E6%9C% ...

  10. HDU 6106 17多校6 Classes(容斥简单题)

    Problem Description The school set up three elective courses, assuming that these courses are A, B, ...