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 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的更多相关文章
- [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 ...
- LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)
653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...
- 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 ...
- 【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; 完
- [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 ...
- 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...
- 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 ...
- LeetCode算法题-Two Sum IV - Input is a BST(Java实现)
这是悦乐书的第280次更新,第296篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第148题(顺位题号是653).给定二进制搜索树和目标数,如果BST中存在两个元素,使得 ...
- [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 ...
随机推荐
- 银联接口C#
银联支付: ChinaPay的会员商户接入支付平台,以方便商户开展网上支付交易. 持卡人从商户网站中生成订单信息,通过公共支付交易平台中的支付网关子系统进行支付的过程,其交易流程包括订单确认.支付处理 ...
- HTML(四)Form标签
<form>…</form> 定义供用户输入的 HTML 表单 例子 <html> <body> <form method="ge ...
- python笔记7-if中的is ;in ;not搭配用法
names="111 222 333" print("111" in names)#返回的是True,用in返回的是布尔值in在里面 print("1 ...
- springMVC拦截css与js等资源文件的解决
写了一个demo的ssm,使用jetty容器跑的,但是在页面的时候总是发现访问资源出现404. 换了多种写法不见效. 偶然发现日志中请求被springMVC拦截了,气死我了. 解决方式: Spring ...
- 杭电多校第七场 1010 Sequence(除法分块+矩阵快速幂)
Sequence Problem Description Let us define a sequence as below f1=A f2=B fn=C*fn-2+D*fn-1+[p/n] Your ...
- NioEventLoop(netty 4.1)
里面有个excecutor属性, 在loopgroup实例化loop的时候, 如果execute一个runnable的task的时候,检测loop启动了没有,没启动的话,执行excecutor的exe ...
- Unity3D使用OpenFileDialog后崩溃
http://ask.unitymanual.com/question/24922 找了很久,原来是我的dll文件引错了,名字都一样,应该引用unity安装目录下的System.Window.Form
- 解决MySQL不允许远程连接的问题
进入MySQL:mysql -u root -p mysql> GRANT ALL privileges ON *.* TO 'root' @'localhost' IDENTIFIED ...
- spring 事务控制 设置手动回滚 TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//假设这是一个service类的片段 try{ //出现异常 } catch (Exception e) { e.printStackTrace(); //设置手动回滚 TransactionAsp ...
- 基于链路的OSPFMD5口令认证
实验要求:掌握基于链路的OSPFMD5口令认证 拓扑如下: 配置如下: R1enable configure terminal interface s0/0/0ip address 192.168.1 ...