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 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
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为用直接用helper函数就可以:dfs(root.left, k - root.val) || dfs(root.right, k - root.val);
但是这样做的问题是一定会把当前的root.val包括进去,然而结果其实可以不包括当前root节点的值
[一句话思路]:
任意取点时,把之前出现过的点先用hashset缓存一下。头一次见。
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
任意取点时,把之前出现过的点先用hashset缓存一下。头一次见。
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
哈希集合缓存
[关键模板化代码]:
DFS,但是要缓存
if(root == null)return false;
//exit: set contains k - root.val;
if (set.contains(k - root.val)) return true;
//store the root.val at present
set.add(root.val);
//expand to left, right
return dfs(root.left, set, k) || dfs(root.right, set, k);
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* 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;
//exit: set contains k - root.val;
if (set.contains(k - root.val)) return true;
//store the root.val at present
set.add(root.val);
//expand to left, right
return dfs(root.left, set, k) || dfs(root.right, set, k);
}
}
653. Two Sum IV - Input is a BST 二叉树版本的更多相关文章
- 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 ...
- 【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; 完
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...
- 【easy】653. Two Sum IV - Input is a BST
BST树求得两个节点的和是target //因为是BST所以中序遍历得到的是递增数组 //这个题的方法就是在一个递增数组中找到两个数的和相加是目标结果 /** * Definition for a b ...
随机推荐
- 抽象工厂模式(abstract)创建型模式
(一)简单工厂模式? 现在的学习是面向对象面向接口的,但是执行时的操作需要实例化后的对象.随着我们需要的类的增加,我们就需要把这些共同的东西提取出来,放在一个抽象类中,让这些子类来继承抽象类.当我们调 ...
- Linux 利用busybox制作根文件系统
busybox版本:1.17.3 官网下载路径:https://busybox.net/downloads/ 网盘下载路径:https://pan.baidu.com/s/1nvrEa73 密码:7y ...
- Mock&Spring集成
Mock&Spring集成 常规Mock单元测试 请参考上一篇文档Mock mock框架的功能性对比 http://jmockit.github.io/MockingToolkitCompar ...
- GDB调试字符数组时指针和数组区别的体现
测试ftell函数时发现报错,先贴源码 // File Name: ftell.c #include <stdio.h> #include <stdlib.h> int mai ...
- 学习 FPGA之前的基础知识
在学习一门技术之前往往应该从它的编程语言入手,比如学习单片机时,往往从汇编或者C语言入门.所以不少开始接触FPGA的开发人员,往往是从VHDL或者Verilog开始入手学习的.但小编认为,若能先结合& ...
- 【转】JMeter使用指南
Abstract 本文重点介绍JMeter工具在测试中地位以及其中一些难以理解或者手册中含糊不清的感念,读者可以通过本文了解这些概念,然后再根据自己的需要查阅JMeter中各个组件的具体用法来完成测试 ...
- 关于sencha touch中给文本添加焦点无效的解决方案
目前的解决方案是给你的执行代码加上一个timeout延迟100ms+ setTimeout(function(){ SoftKeyboard.isShowing(function(isShowing) ...
- PolyBase 指南
PolyBase 是一种可通过 t-sql 语言访问数据库外部数据的技术.PolyBase is a technology that accesses data outside of the data ...
- OrhtoMCL 使用方法
OrthoMCL的使用分13步进行,如下: 1. 安装和配置数据库 Orthomcl可以使用Oracle和Mysql数据库,而在这里只介绍使用Mysql数据库.修改配置文件/etc/my.cnf,对M ...
- from 组件
知识补充 : location.href="/index/" 加路径或者网址都可以 location.h ...