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

看的其他coder给出的答案,很详细。

方法1

Two sum问题可以转化为查找一个数值问题,Target-node.val,主要思想是使用Hashset存储二叉搜索树的节点,对BST进行遍历,当每次插入一个节点时候,判断set中是否存Target-node.val。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean findTarget(TreeNode root, int k) {
HashSet<Integer> set = new HashSet();
return dfs(root,set,k); }
public boolean dfs(TreeNode root, HashSet<Integer> set ,int k)
{
if(root == null) return false;
if(set.contains(k - root.val)) return true;
set.add(root.val);
return dfs(root.left, set, k) || dfs(root.right, set, k);
}
}

方法2

这种方法和常规的TwoSum问题类似,先得到排序的数组,恰好二叉搜索树的中序遍历就是有序的数组,使用两个指针分别指向数组的首和尾,判断nums[i]+nums[j]的和与Target的关系。
   public boolean findTarget(TreeNode root, int k) {
List<Integer> nums = new ArrayList<>();
inorder(root, nums);
for(int i = 0, j = nums.size()-1; i<j;){
if(nums.get(i) + nums.get(j) == k)return true;
if(nums.get(i) + nums.get(j) < k)i++;
else j--;
}
return false;
} public void inorder(TreeNode root, List<Integer> nums){
if(root == null)return;
inorder(root.left, nums);
nums.add(root.val);
inorder(root.right, nums);
}
 
 
 
 
 

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

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

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

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

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

  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. 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)

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

  9. 【easy】653. Two Sum IV - Input is a BST

    BST树求得两个节点的和是target //因为是BST所以中序遍历得到的是递增数组 //这个题的方法就是在一个递增数组中找到两个数的和相加是目标结果 /** * Definition for a b ...

随机推荐

  1. ERP中通过自定义单打开流程图

    背景: AIO75系统中,制作流程图时选择所属模块,即可在对应模块的左侧列表展示流程图入口. 但在AIO5商务版中没有相关入口,故本文提供使用自定义菜单的方式挂出流程图. 具体步骤: 1.先去看一下是 ...

  2. ABAP中的AMDP(ABAP-Managed Database Procedures )

    ABAP管理下的数据库存储过程(ABAP-Managed Database Procedure,以下简称AMDP)是在APAP on SAP HANA开发中的一种优化模式.AMDP使用数据库语言书写, ...

  3. null与undefined的比较

    null在JavaScript中是关键字,它属于一个特殊的值,即空值. 而undefined它不是关键字,它表示未定义,属于预定义的全局变量. null == undefined 返回的是 true  ...

  4. 【原创精品】程序员最强大的利器——电子笔记本的思考(1)(ver0.3)

    [原创精品]程序员最强大的利器,本文以下内容全都是作者EverStenis(胡佳吉)的原创,未经授权不得转载,抄袭必究. 我想问大家一个问题,对于我们程序员来说,在我们的武器工具库中,最强大的一件利器 ...

  5. 爬起点小说day03

    # 把所有类别的前3页的小说爬取下来 import scrapyfrom scrapy.http import Requestfrom time import sleepfrom qidianNove ...

  6. J2SE-包装类

    目录 1 为什么提供包装类? 2 装箱和拆箱 3 包装类的4个特点 4 包装类类型 正文 1 为什么提供包装类? 1) 由于Java的基本数据类型功能简单,不具备面向对象的特性,实际使用时存在很多的不 ...

  7. PHPstorm 如何新增项目

    如何在PHPstorm新增项目 1.打开设置 2.找到Directories ,点击增加路径

  8. Js 调用 WebService 实例

    Html页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/ ...

  9. tcpdum使用详解

      http://starsliao.blog.163.com/blog/static/89048201062333032563/ TCPdump抓包命令 tcpdump是一个用于截取网络分组,并输出 ...

  10. Azure ARM (18) 将传统的ASM VM迁移到ARM VM (1)

    <Windows Azure Platform 系列文章目录> 目前很多客户陆续的把传统ASM VM迁移至ARM VM.我这里简单介绍一下. 整个迁移过程分为: 1.Validate,Az ...