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 ...
随机推荐
- visual studio 打开微软MVC3示例MvcMusicStore的详细修改方法
1.官方下载地址:http://mvcmusicstore.codeplex.com/ 2.直接打开项目后,引用中会有三个dll文件报错,分别是System.Web.MVC;System.Web.He ...
- EF+MVC学习中的不理解的问题
1.之所以被定义为virtual便于实现延迟加载 代码: public virtual ICollection<Enrollment> Enrollments { get; set; } ...
- git生成sshkey
- 记录linux tty的一次软锁排查2
在复现tty的死锁问题的时候,文洋兄使用了如下的方式: #include <fcntl.h> #include <unistd.h> #include <stdio.h& ...
- 20170505 PHP实践中知识点
1.json_encode 不转义 2.empty() 与 isset() 区别 在使用 php 编写页面程序时,我经常使用变量处理函数判断 php 页面尾部参数的某个变量值是否为空,开始的时候我习惯 ...
- CentOS7.x机器安装Azure CLI2.0
安装Azure CLI 2.0的前提是:机器中必须有 Python 2.7.x 或 Python 3.x.如果机器中没有其中任何一个Python的版本,请及时安装 1.准备一台CentOS 7.3的机 ...
- Servlet--ServletInputStream类,ServletOutputStream类
ServletInputStream类 定义 public abstract class ServletInputStream extends InputStream 这个类定义了一个用来读取客户端的 ...
- LAMP_yum安装
前言,人总是会越来越懒,说真的,我是摸着良心说话的 开始总是喜欢源码安装,因为可以定制,而且能显得有格调(逼格),但是一安装就要半天,还有各种依赖包的安装,各种报错,不忍直视 下面是我摘自晚上的一篇l ...
- zabbix监控-percona监控MySQL(三)
监控MySQL实战 标签(linux): zabbix & mysql 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 percona为MySQL制作了专 ...
- 程序管理与SElinux
一.程序: 1.在Linux中,触发任何一个事件是,系统都会将他定义为一个程序,并且给予这个程序一PID,同时依据启发这个程序的使用者与相关属性关系,给予这个PID一组有效的权限设定,从此以后,这个P ...