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 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; }
* }
*/
public class Solution {
public boolean findTarget(TreeNode root, int k) {
if (root == null)
return false;
List<Integer> arr = new ArrayList<Integer>();
toArr(root, arr);
int head = 0, tail = arr.size()-1;
while (head < tail) {
if (arr.get(head)+arr.get(tail) == k)
return true;
else if (arr.get(head)+arr.get(tail) < k) {
head ++;
}
else tail --;
}
return false;
} private void toArr(TreeNode node, List<Integer> arr) {
if (node == null)
return ;
toArr(node.left, arr);
arr.add(node.val);
toArr(node.right, arr);
} }
LeetCode - 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
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 解题报告
题目要求 Given a Binary Search Tree and a target number, return true if there exist two elements in the ...
- 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...
- [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 ...
- 【leetcode】653. Two Sum IV - Input is a BST
Given the root of a Binary Search Tree and a target number k, return true if there exist two element ...
- 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 ...
随机推荐
- Spring MVC集成Swagger
什么是Swagger? 大部分 Web 应用程序都支持 RESTful API,但不同于 SOAP API——REST API 依赖于 HTTP 方法,缺少与 Web 服务描述语言(Web Servi ...
- __block __weak
IOS BLOCK收集 在ios,blocks是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.它和传统的函数指针很 ...
- IDEA关掉重复代码波浪线
如图: File----Settings
- Centos7-卸载自带的jdk 安装jdk8
卸载JDK Centos7一般都会带有自己的openjdk,我们一般都回用oracle的jdk,所以要卸载 步骤一:查询系统是否以安装jdk #rpm -qa|grep java 或 #rpm ...
- Spring-事务配置和详解
一.Spring事务配置 在项目开发过程中经常会使用事务来确保数据的一致性.根据网上的资料整理一下在spring中配置事务的几种方式.无论是哪种方式都需要在配置文件中配置连接池和事务管理器,代码如下. ...
- linux_发邮件
如何通过linux发邮件? 邮箱配置文件: /etc/mail.rc 1. 邮箱文件配置 vim /etc/mail.rc # 添加一下数据 set from=beimen@163.com smtp= ...
- Effective Java 第三版——30. 优先使用泛型方法
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- @@IDENTITY详细测试
今天看数据库SQL,有发现存储过程中有使用到SCOPE_IDENTITY()这个函数,后来问了下谷歌大婶,搜到一个比较重要的博客,链接如下:https://dotblogs.com.tw/kkman0 ...
- win7下JAVA环境变量配置方法
1.首先,根据自己的需要下载1.6或者1.7的JDK,安装JDK.(安装的时候记一下安装目录,后面会用到) 2.右键计算机→属性→高级系统设置→高级→环境变量,在系统变量部分新建名为"JAV ...
- MyEclipse设置jsp页默认打开方式
可以用来设置jsp页默认打开是代码编辑模式而不是半视图半代码的模式. 1.选择菜单Window→Preferences. 2.选择General→Editors→File Associations.在 ...