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的更多相关文章

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

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

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

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

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

  6. 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)

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

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

  8. 【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 ...

  9. 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. DFS算法(——模板习题与总结)

    首先,需要说明的是搜索算法本质上也是枚举的一种,时间复杂度还是很高的,遇到问题(特别是有水平的比赛上),不要优先使用搜索算法. 这里总结一下DFS算法: 1.从图中某个顶点出发,访问v. 2.找出刚访 ...

  2. 地图API地址  百度地图开放平台

    http://lbsyun.baidu.com/index.php?title=jspopular

  3. {style}/index_article.htm {style}表示什么意思啊

    LS有点安全意识好不好.... 在你的后台系统设置有个"模板默认风格:________  cfg_df_style " 默认是default也就是 {style}=模板路径+模板默 ...

  4. 怎么获取smtp服务器用户帐号和密码

    在OE里工具-帐户..-添加-邮件 打开选项卡,依次填好,昵称,按下一步,邮箱地址,按下一步,填POP和SMTP服务器地址,按下一步,按用户名和密码,再按下一步就设置好了.有些邮件服务器在发信的时候, ...

  5. 数据库连接池(c3p0)

    (一)问题的提出: 在使用开发基于数据库的web程序时,传统的数据库使用模式按照以下步骤: 在程序中建立数据库连接 进行sql操作 断开数据库连接 但是,这种模式存在着移动的问题: 传统连接模式每次向 ...

  6. Github终于连上了hexo

    2018-01-2722:59:28 我的妈呀,看看这感人的网速,哎不想吐槽在中国连外网的速度 总结一下连接过程吧 漫漫长征路,难的要死. 一.github的注册和使用不再详述 二.Git Desk ...

  7. Can’t open /dev/* exclusively. Mounted filesystem?解决

    1 错误提示:Can’t open /dev/* exclusively. Mounted filesystem? 做完软件RAID之后,根据鸟哥书上的操作,其实没有真正删除软件RAID,导致出现如下 ...

  8. Servlet--Servlet接口

    servlet主要数据结构 Servlet 接口:主要定义了servlet的生命周期方法 ServletConfig接口:为servlet提供了使用容器服务的若干重要对象和方法. ServletCon ...

  9. 网络编程之TCP编程

    网络编程之TCP编程 前面已经介绍过关于TCP协议的东西,这里不做赘述.Java对于基于TCP协议的网络通信提供了良好的封装,Java使用socket对象来代表两端的通信窗口,并通过Socket产生I ...

  10. 程序员之殇 —— (Are you afraid of me? Don't be.)灵感=神秘感

    Are you afraid of me? (你们怕我吗?) Don't be.(不用怕) I am a programmer who just won't die.(我是不会死的程序员) 自从跟踪到 ...