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 ...
随机推荐
- bzoj 4815 [Cqoi2017]小Q的表格——反演+分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4815 大概就是推式子的时候注意有两个边界都是 n ,考虑变成 2*... 之类的. 分块维护 ...
- java的时间
先看例子: import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; imp ...
- couchdb的使用例子
couchdb安装 sudo apt-get install erlang sudo apt-get install libmozjs185-dev libicu-dev 下载源码,编译安装 启动以后 ...
- m'ybatis 一对一 一对多 配置详解
javabean: package com.me.model; import java.io.Serializable; import java.util.Date; import java.util ...
- 皆在FPGA之外
最近做电力方面的项目,由于跨行业,所以很长一段时间都在做前期准备工作. 项目设计前应尽量做到面面俱到,否则会在项目设计中遇到下面大概率问题: 性能不满足需求,然后为了提升性能,资源又成了瓶颈: 功能设 ...
- Hibernate学习9—检索策略
本章,采用Class和Student —— 1 对 多的关系: Student.java: package com.cy.model; public class Student { priv ...
- jmeter数据关联_后置处理器_正则表达式提取器
- mysql 优化(3)
using filesort 不能利用索引来进行分组或排序,利用filesort算法在内存或者磁盘进行排序using temporary 先在内存中进行分组,归并等操作,不够利用磁盘 SELECT i ...
- python基础知识之列表、元祖、字典、集合、字符串。
1.可变类型之列表 列表用 [ ]来定义是可变的,可以通过索引值来去查询里面的字段可以可以追加,删除等 names='zhangyang guyun xiangpeng xuliangwei' nam ...
- SpringBoot起步依赖和自动配置
一.起步依赖 1. 是什么 本质上是一个Maven项目对象模型(Project Object Model, POM), 定义了对其他库的传递依赖,这些东西加在一起即支持某项功能. 比如: spring ...