题目地址:Two Sum IV - Input is a BST - LeetCode


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   7Target = 9Output: True

Example 2:

Input: 5/ \3   6/ \   \
2   4   7Target = 28Output: False

这道题目是Two Sum的进阶,最简单的办法是用set,穷举遍历。
Python解法如下:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:def findTarget(self, root: TreeNode, k: int) -> bool:def find(root, k, s):if root == None:return Falseif root.val in s:flag = 1return Trues.add(k-root.val)return find(root.left, k, s) or find(root.right, k, s)s = set()return find(root, k, s)

时间复杂度为O(n),空间复杂度为O(n)。

稍微好一点的做法是一层一层遍历,这样可以迭代解决。

LeetCode 653. Two Sum IV - Input is a BST--Python解法相关推荐

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

  2. 653. Two Sum IV - Input is a BST

    题目来源: 自我感觉难度/真实难度: 题意: 分析: 自己的代码: class Solution(object):def findTarget(self, root, k):""& ...

  3. C#LeetCode刷题之#653-两数之和 IV - 输入 BST(Two Sum IV - Input is a BST)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4098 访问. 给定一个二叉搜索树和一个目标结果,如果 BST 中 ...

  4. 【动态规划】LeetCode 377. Combination Sum IV

    LeetCode 377. Combination Sum IV Solution1: 我的未能AC的答案 题目描述的和前几道题差不多,但实际上不能用DFS来做(会超时),要用动态规划,还是记录一下吧 ...

  5. LeetCode 动态规划(Dynamic programming)系列题目--C++,Python解法

    LeetCode上有许多态规划(Dynamic programming)的题目,我在这里整合一下 本文章不再更新,请看LeetCode 所有题目总结 LeetCode 所有题目总结:LeetCode ...

  6. LeetCode 167. Two Sum II - Input array is sorted--Python解法

    题目地址:Two Sum II - Input array is sorted - LeetCode Given an array of integers that is already sorted ...

  7. LeetCode 204. Count Primes--从一开始的质数个数--Python解法--面试算法题

    题目地址:Count Primes - LeetCode Count the number of prime numbers less than a non-negative number, n. E ...

  8. LeetCode 141. Linked List Cycle--面试编程题--C++,Python解法

    题目地址:Linked List Cycle - LeetCode Given a linked list, determine if it has a cycle in it. To represe ...

  9. leetcode 377. Combination Sum IV | 377. 组合总和 Ⅳ(动态规划)

    题目 https://leetcode.com/problems/combination-sum-iv/ 题解 最近养成了上来直奔 Related Topics 的习惯- 确认过眼神,又是个 dp 问 ...

最新文章

  1. 发改委:互联网企业没有出现大规模裁员现象
  2. TCP/IP总结(4)TCP 之数据包格式
  3. 关闭sql执行功能及找回08CMS系统管理员密码
  4. java的格式化时间工具类
  5. eclipse 设置 content type 编码格式
  6. 可以部署在广域网执行QQ高仿版 GG2014 (源代码)
  7. linq to sql 多条件组合查询
  8. iPhone使用CoreTelephony获得SIM卡网络运营商资讯和通话资料
  9. Unity基础知识学习笔记二
  10. C语言(第二章):数据类型、运算符、表达式
  11. HDU 1532 Drainage Ditches(poj1273)【E-K 最大流】
  12. python是什么类型的语言-编程语言分类及python所属类型
  13. [转载] strtol() -- 将字符串转换成长整型数(转载)
  14. Linux进程的管理与调度(四) -- Linux下的进程类别以及其创建方式
  15. PDF怎么转换成PPT?用迅读PDF大师,轻松解决教案问题
  16. Android webview缓存机制
  17. 模块化机房建设指导书_模块化机房建设方案解析
  18. 淘宝自动查券找券返利机器人搭建教程
  19. 进度猫带你来了解,一个优秀的管理者都有哪些准则
  20. 读书笔记:Dynamic GCN: Context-enriched Topology Learning for Skeleton-based Action Recognition

热门文章

  1. RDKit | 通过分析活性化合物确定指标阈值
  2. VMware虚拟机上的文件如何和Windows进行共享
  3. You must restart adb and Eclipse问题的解决
  4. mysql 转pxc_PXC 配置笔记-从MySQL直接转成PXC集群
  5. 人类是怎么从猩猩身上惹来艾滋病的?人与兽的关系很单纯!
  6. ggplot2笔记6:标度、轴和图例
  7. java 传入参数_java参数怎么传递参数
  8. R语言使用ggplot2包的快速可视化函数qplot绘制散点图实战
  9. pandas使用groupby函数和cumsum函数计算每个分组内的数值累加值、并生成新的dataframe数据列( cumulative sum of each group in dataframe
  10. R语言使用reshape2包的melt函数将dataframe从宽表到长表(Wide- to long-format)、指定行标识符变量、并自定义生成的长表的标识符列的名称