Java实现 LeetCode 653 两数之和 IV - 输入 BST(递归,找差值)
653. 两数之和 IV - 输入 BST
给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定的目标结果,则返回 true。
案例 1:
输入:
5
/ \
3 6
/ \ \
2 4 7
Target = 9
输出: True
案例 2:
输入:
5
/ \
3 6
/ \ \
2 4 7
Target = 28
输出: False
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
boolean found = false;
public boolean findTarget(TreeNode root, int k) {
inorder(new HashSet(),root,k);
return found;
}
private void inorder(Set<Integer> set,TreeNode root,int k){
if(root==null || found) return;
inorder(set,root.left,k);
set.add(root.val);
if(set.contains(k-root.val) && root.val!=k-root.val){
found = true;
}
inorder(set,root.right,k);
}
}
Java实现 LeetCode 653 两数之和 IV - 输入 BST(递归,找差值)的更多相关文章
- LeetCode 653. 两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)
653. 两数之和 IV - 输入 BST 653. Two Sum IV - Input is a BST 题目描述 给定一个二叉搜索树和一个目标结果,如果 BST 中存在两个元素且它们的和等于给定 ...
- Leetcode 653. 两数之和 IV - 输入 BST
题目链接 https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/ 题目描述 给定一个二叉搜索树和一个目标结果,如果 B ...
- 653. 两数之和 IV - 输入 BST + HashSet
653. 两数之和 IV - 输入 BST 题目描述 题解分析 最简单的方法就是遍历整棵树,找出所有可能的组合,判断是否存在和为 kk 的一对节点.现在在此基础上做一些改进. 如果存在两个元素之和为 ...
- Java实现 LeetCode 167 两数之和 II - 输入有序数组
167. 两数之和 II - 输入有序数组 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必 ...
- [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 ...
- LeetCode653. 两数之和 IV - 输入 BST
题目 直接暴力 1 class Solution { 2 public: 3 vector<int>ans; 4 bool findTarget(TreeNode* root, int k ...
- Java实现 LeetCode 1两数之和
1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...
- Leetcode 167. 两数之和 II - 输入有序数组 By Python
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的下标值 ...
- LeetCode 167. 两数之和 II - 输入有序数组
题目: 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值index1 和 index2,其中 index1 必须小于 index2. 说明: 返回的 ...
随机推荐
- Linux从error while loading shared libraries: libxxx.so.x 错误的常规解决思路看程序与动态库的关系
出现这类错误的原因通常是动态库无法被加载,本文介绍了常规的解决方案,适用多种情况: 创作不易,如果本文帮到了您: 如果本文帮到了您,请帮忙点个赞
- STM32 IAP 升级官方资料汇总
整理了一下SMT32标准外设库进行IAP升级的官方demo: 标准库版本 STM32F10xxx in-application programming using the USART (AN2557) ...
- Properties集合与IO流
package com.itheima.demo07.Prop; import java.io.FileOutputStream; import java.io.FileReader; import ...
- 风扇转速通过FPGA采样
1.风扇最大转速16000RPM,那么每一转需要时间60S/16000=0.00375S=375*10^4ns=T=T1+T2+T3+T4: 2.采样0.6S内的风扇detect信号的上升沿个数:0. ...
- [hdu5445 Food Problem]多重背包
题意:一堆食物,有价值.空间.数量三种属性,一些卡车,有空间,价格,数量三种属性.求最少的钱(不超过50000)买卡车装下价值大于等于给定价值的食物,食物可以拆开来放. 思路:这题的关键是给定的条件: ...
- c#得出两个列表的交集
c#提供了Intersect来得到两个列表的交集,它是通过使用默认的相等比较器对值进行比较生成两个序列的交集,定义为: public static IEnumerable<TSource> ...
- java方法调用顺序
public class JavalearningApplicationTests { static { System.out.println("Test的静态代码块"); } p ...
- 这或许是最详细的JUC多线程并发总结
多线程进阶---JUC并发编程 完整代码传送门,见文章末尾 1.Lock锁(重点) 传统 Synchronizd package com.godfrey.demo01; /** * descripti ...
- java ->IO流_转换流
转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者OutputStre ...
- 基于hexo创建博客(Github托管)
基于hexo的博客 搭建好的博客网站 dengshuo7412.com 搭建步骤 1.依赖文件下载 Node.js 2.Hexo的安装 3.部署到Github 4.Hexo创建博客基本操作 5.Hex ...