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
 思路:把值放入map中,遍历值,并判断另一个组合数是否在map中即可。
JAVA CODE
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
Map<Integer,Integer> map = new HashMap<>();
int key = 0;
public boolean findTarget(TreeNode root, int k) {
// Queue<TreeNode> queue = new ArrayDeque<>();
// queue.offer(root);
// while(!queue.isEmpty()){
// root =
// if()
// }
find(root);
for (Integer value : map.values()) {
if(value*2!=k&&map.containsValue(k - value.intValue()))
return true;
}
return false;
}
void find(TreeNode rr){
if(rr != null){
find(rr.left);
map.put(new Integer(key++), new Integer(rr.val));
find(rr.right);
}
}
}

Two Sum IV - Input is a BST的更多相关文章

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

  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_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; 完

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

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

  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. [Swift]LeetCode653. 两数之和 IV - 输入 BST | 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 ...

  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. ThinkPHP自定义分页模板

    TpPageHelper.php <?php namespace tool; use think\Paginator; class TpPageHelper extends Paginator ...

  2. hashlib使用时出现: Unicode-objects must be encoded before hashing

    # hashlib.md5(data)函数中,data参数的类型应该是bytes# hash前必须把数据转换成bytes类型>>> from hashlib import md5 F ...

  3. sql执行机制

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp38 sql执行机制 1.对于普通的sql语句只有where条件的执行机制 ...

  4. 如何写SysV服务管理脚本

    本文目录: 1.1 SysV脚本的特性1.2 SysV脚本要具备的能力1.3 start函数分析1.4 stop函数分析1.5 reload函数分析1.6 status.restart.force-r ...

  5. .net core的在初始化数据的拦截处理

    本人初接触 .net core 如有不对的地方,请大家随时指正,共同学习. 首先说明,此案例是基于.net core1.0版本的,对于2.0好多的功能已经升级,例如:一些常用的dll已经在框架中存在, ...

  6. C# 索引器简介

    索引器是一种特殊的类成员,它能够让对象以类似数组的方式来存取,是程序看起来更为直观,更容易编写. 1.索引器的定义 C#中的类成员可以是任意类型,包括数组和集合.当一个类包含了数组和集合成员时,索引器 ...

  7. 个人作业2——必应词典APP分析

    第一部分:调研.评测 1.刚刚打开必应词典的时候,它给我的第一反应就是界面美观,最上面是一个查询框,下面有一些经典的句子.单词以及一些精选的文章,所有的功能都可以一目了然,看一眼就知道要怎么去使用,这 ...

  8. 【Beta阶段】测试与发布

    [Beta阶段]测试报告 一.Bug记录 1. 已经修复的BUG:文件查重的小组的空指针处理了 . 2.未能修复的bug: (1).在进行查重的时候必要要有10个文件,不然会报错:        (2 ...

  9. 201521123045 《Java程序设计》第8周学习总结

    第08周-集合与泛型 1. 本周学习总结 2. 书面作业 1.List中指定元素的删除(题目4-1)1.1 实验总结 答: Scanner实现字符串的输入有两种方法,一种是next(),一种nextL ...

  10. Java课程设计博客(团队)

    Java课程设计博客(团队) 1. 团队/项目名称 使用JAVA实现简易HTTP服务器 2. 团队成员 组长:林一心 组员:张杭镖 3. 项目git地址 https://github.com/oran ...