题目要求

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#中用ILMerge合并DLL和exe文件成一个exe文件或者DLL

    ILMerge是一个将多个.NET程序集合并到一个程序集中的实用程序.它既可以作为  开源使用,也可以作为NuGet包使用. 如果您在使用它时遇到任何问题,请与我们联系.(mbarnett _at_ ...

  2. 库数据有则修改,无则新增sql

    方法一:insert中on duplicate key update的使用 例如: <insert id="bindPlaceInfo"> insert into gr ...

  3. highcharts的dataLabels如何去处阴影

    问题: 在使用highcharts生成的图标中dataLabels是有阴影的,通常是影响美观,那么如何去除阴影呢? 原因:是因为highcharts将dataLabels生成的标签是tspan,里面有 ...

  4. 小程序 -- ui布局

    Flex布局 相对定位和绝对定位 弹性盒模型 display flex-direction flex-wrap  :nowrap(不换行)/ wrap(换行,第一行在上方)/ wrap-reverse ...

  5. type=file的inpu美化,自定义上传按钮样式

    <div class="div1"> <div class="div2">点击上传</div> <input type ...

  6. How to fetch all Git branches

    问题描述 I cloned a Git repository, which contains about five branches. However, when I do git branch I ...

  7. Swift 统计项目中所有按钮的点击次数

    class Swizzle: NSObject { override class func load() { UIButton.xxx_swizzleSendAction() } } extensio ...

  8. Stock Chase 拓扑

    题意 给出n个公司  m条信息 当某条信息构成环了  则这条信息是错误的 统计有多少个信息是错误的 这题是一条一条读入  虽然分在拓扑排序类里面 但是不会用拓扑排序来做 可以用floyd思想来做 如果 ...

  9. JVM内存简单总结

    根据自己的认识,简单总结下Java中的数据存储及内存分析. Java中的内存大致可以分为三块:栈内存.堆内存.方法区内存,看图说话. 1).栈 栈(stack):栈是限定仅在表头进行插入和删除操作的线 ...

  10. [MNIST数据集]输入图像的预处理

    因为MNIST数据是28*28的黑底白字图像,而且输入时要将其拉直,也就是可以看成1*784的二维张量(张量的值在0~1之间),所以我们要对图片进行预处理操作,是图片能被网络识别. 以下是代码部分 i ...