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. macaca 测试web(2)

    还是以博客园登录为例, 可以查看上一篇上一篇 这次只测试登录,使用ddt的数据驱动,这里我博客也有记录 地址 其实我在uiautomator +python 实现安卓自动化 一文当中我已经使用过ddt ...

  2. Bootstrap-table使用记录(转)

    HTML代码 /*index.cshtml*/ @section styles{ <style> .main { margin-top:20px; } .modal-body .form- ...

  3. Javascript的内容摘要

    JS简介和变量 {JS的三种方式}            1 HTML中内嵌JS(不提倡使用)            <button onclick="javascript:alert ...

  4. Junit单元测试实例

    1.非注解 public class Test { @org.junit.Test public void testPrice() { ClassPathXmlApplicationContext b ...

  5. sqlserver自定义函数

    标量函数 RETURNS 子句指定一种标量数据类型,则函数为标量值函数. 语法 Create function 函数名(参数) Returns 返回值数据类型 [with {Encryption | ...

  6. VHDL学习记录

    VHDL全名Very-High-Speed Integrated Circuit Hardware Description Language ,是一种标准硬件描述语言.下面通过60进制计数器来分析VH ...

  7. linq 起源

    在说LINQ之前必须先说说几个重要的C#语言特性 一:与LINQ有关的语言特性 1.隐式类型 (1)源起 在隐式类型出现之前, 我们在声明一个变量的时候, 总是要为一个变量指定他的类型 甚至在fore ...

  8. 微信小程序view标签以及display:flex的测试

    一:testview.wxml,testview.js自动生成示例代码 //testview.wxml <view class="section"> <view ...

  9. 团队作业八——第二次团队冲刺(Beta版本)第7天&项目汇总

    项目汇总 第一天:http://www.cnblogs.com/newteam6/p/6879383.html 第二天:http://www.cnblogs.com/newteam6/p/688078 ...

  10. 团队作业4——第一次项目冲刺(Alpha版本) Day6

    首先和助教及老师表示抱歉,博客确实当时就写了,但是一直不算写好,因为这几天卡住了,预计实现的功能实现不了,进度跟不上,现在也在寻求解决方法. 1.站立式会议: 2. Leangoo任务分解图: 3.任 ...