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. window有哪写事件?

    onload:加载事件网页加载完毕后执行. onscroll:滚动事件. onresize:窗口缩放事件.

  2. macOS Sierra 如何打开任何来源

    1.打开应用程序-实用工具-终端: 2.复制以下代码(红色处注意是两个-)到终端中,回车(输入电脑密码): sudo spctl --master-disable 3.打开应用程序-系统偏好设置-安全 ...

  3. RabbitMQ 均衡调度(公平分发机制)

    均衡调度是针对Consumer来说的.现在有两个Consumer请求同一个队列的消息.RabbitMQ会将序号为奇数的消息发给第一个Consumer,会将序号为偶数的消息发送给第二个Consumer. ...

  4. 第一篇 入门必备 (Android学习笔记)

    第一篇 入门必备 第1章 初识Android 第2章 搭建你的开发环境 第3章 创建第一个程序--HelloWorld 第4章 使用Android工具   ●Android之父 Android安迪·罗 ...

  5. 安装ubuntu gnome桌面

    注意: ubuntu 14.04.5默认的为unity桌面,有多点触发,没有自带Tweak Tool工具. 安装gnome桌面 sudo apt-get install ubuntu-gnome-de ...

  6. BeanUtils.copyProperties方法,当属性Date为null解决

    问题描述:org.apache.commons.beanutils user对象和formBean对象都有属性birthday,而且都是java.sql.Date类型的 当进行BeanUtils.co ...

  7. jvm的基本结构以及各部分详解(转)

    原文链接:https://www.cnblogs.com/zwbg/p/6194470.html 1.java虚拟机的基本结构 图: 1.类加载器子系统从文件系统或者网络中加载Class信息,类信息( ...

  8. day 54 jQuery 的初步基础

    jQuery介绍 jQuery是一个轻量级的.兼容多浏览器的JavaScript库. jQuery使用户能够更方便地处理HTML Document.Events.实现动画效果.方便地进行Ajax交互, ...

  9. Android:AndroidManifest.xm中xmlns的作用

    有了它,你就可以alt+/作为提示,提示你输入什么,不该输入什么,什么是对的,什么是错的,也可以理解为语法文件.或者语法判断器什么的 这个主要作用是在运行的时候那些控件的属性都是通过它来识别的,如果上 ...

  10. mysql三范式

    第一范式:有主键,具有原子性,字段不可分割. 第二范式:完全依赖,没有部分依赖. 第三范式:没有传递依赖. 总结:数据库设计尽量遵循三范式,但是还是根据实际情况进行取舍,有时候会拿冗余还速度,最总用的 ...