题目要求

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.

题目分析及思路

给定一个二叉搜索树和一个目标值,若该树中存在两个元素的和等于目标值则返回true。可以先获得树中所有结点的值列表,然后遍历每个值,判断目标值与该值的差是否也在该列表中,并保存每个值的判断结果。若有一个true则返回true。

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:

vals = []

q = collections.deque()

q.append(root)

while q:

size = len(q)

for _ in range(size):

node = q.popleft()

if not node:

continue

vals.append(node.val)

q.append(node.left)

q.append(node.right)

ans = []

for v in vals:

if k-v in vals and k-v != v:

ans.append(True)

else:

ans.append(False)

if True in ans:

return True

else:

return False

LeetCode 653 Two Sum IV - Input is a BST 解题报告的更多相关文章

  1. 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)

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

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

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

  6. 【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; 完

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

  8. 【leetcode】653. Two Sum IV - Input is a BST

    Given the root of a Binary Search Tree and a target number k, return true if there exist two element ...

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

随机推荐

  1. C语言malloc函数为一维,二维,三维数组分配空间

    c语言允许建立内存动态分配区域,以存放一些临时用的数据,这些数据不必在程序的声明部分定义,也不必等到函数结束时才释放,而是需要时随时开辟,不需要时随时释放,这些数据存储在堆区.可以根据需要,向系统申请 ...

  2. python学习第25天

    异常处理 什么是异常?什么是错误? 1,程序中难免出现错误. 错误主要分为两种: 1,语法错误 语法错误是根本上的错误,无法通过PYTHON解释器.完全无法执行,是在程序中不应该出现的错误.无法进行异 ...

  3. 忘记加入spring-aop-4.3.16.RELEASE.jar出错

    出错代码: java.lang.NoClassDefFoundError: org/springframework/aop/framework/AopProxyUtils at org.springf ...

  4. Redis数据类型List

    Redis的List是通过Linked List(链表)来实现的String集合,所以插入数据的速度很快.但是缺点就是在数据量比较大的时候,访问某个数据的时间可能会很长,但针对这种情况,可以使用Sor ...

  5. 五分钟快速掌握RPC原理及实现

    随着公司规模的不断扩大,以及业务量的激增,单体应用逐步演化为服务/微服务的架构模式, 服务之间的调用大多采用rpc的方式调用,或者消息队列的方式进行解耦.几乎每个大厂都会创建自己的rpc框架,或者基于 ...

  6. python 代理

    1.参考 http://docs.python-requests.org/en/master/user/advanced/ Using Python’s urllib2 or Requests wit ...

  7. 编码 ASCII, GBK, Unicode+utf-8

    0. 1.参考 网页编码就是那点事 阮一峰 字符编码笔记:ASCII,Unicode 和 UTF-8 2.总结 美国 ASCII 码 发音: /ˈæski/ :128个字符,只占用了一个字节的后面7位 ...

  8. Python2还是Python3

    Python2还是Python3 相信很多新接触Python的人都会纠结这一个问题,学Python2还是Python3? 不像Java一样每个新版本基本都是基本兼容以前的版本的.Python2和Pyt ...

  9. docker-- 卷

    进入容器centos终端中 #docker   attach   c1 在 容器中安装nginx #yum   install  epel-relesae #yum  install  nginx # ...

  10. Python学习(三十三)—— Django之ORM

    Object Relational Mapping(ORM) 一.ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系 ...