题目要求

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. 肺结节CT影像特征提取(四)——肺结节CT影像特征提取MATLAB代码实现

    之前的文章讲述了肺结节CT影像数据特征提取算法及基于MATLAB GUI设计的肺结节CT影像特征提取系统.本文将讲述几个主要部分的代码实现,分别是预处理.灰度特征提取.纹理特征提取.形态特征提取数据. ...

  2. git知识总结-1.git基础之基本术语

    1.前言 git是一种分布式版本管理工具,本文主要是通过阅读博客中几篇讲述git的优秀文章,并对文章进行整理.提炼总结得出一份git的说明文档. 本文档介绍了git的基本原理及常用操作,目标是通过阅读 ...

  3. C++的一些小Tip

    string转数字: 一种是转换为char*后再使用atoi:atoi(s.c_str()).这个方法的神奇之处在于,如果s是负数也能顺利转化,但是,在leetcode显示,自己先判断是不是负数的话计 ...

  4. django2.0无法加载外部css和js的问题

    解决问题的思路来源于https://www.v2ex.com/t/430192 先是创建static目录,该目录与manage.py同级 然后在项目settings.py文件里添加 STATICFIL ...

  5. C++设计模式——迭代器模式

    前言 最近非常感伤,总是怀念大学的日子,做梦的时候也常常梦到.梦到大学在电脑前傻傻的敲着键盘,写着代码,对付着数据结构与算法的作业:建立一个链表,遍历链表,打印链表.现在把那个时候声明的链表的头文件拿 ...

  6. DataStructure-链表实现指数非递减一元多项式的求和

    // 2-链表实现多项式的求和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<stdio.h> #inclu ...

  7. mycat 测试主从读写分离

    下载解压及创建用户组和目录属性 下载地址:1.https://github.com/MyCATApache/Mycat-download.2.http://dl.mycat.io/ wget http ...

  8. [C][变量作用域]语句块

    概述 C语言作用域有点类似于链式结构,就是下层能访问上层声明的变量,但是上层则不能访问下层声明的变量: #include <stdio.h> #define TRUE 1 int main ...

  9. 微信开发getLocation、openLocation等一些功能不起作用,但是走ready方法 closeWindow一些方法可以用

    1.检查wx.config,发现我在jsApiList也声明了这些方法,并且也走了ready回调 wx.config({ debug: false, // 开启调试模式,调用的所有api的返回值会在客 ...

  10. IdentityServer4 密码模式认证

     授权服务器设置 添加用户 添加测试用户,也可以从数据库查 public static List<TestUser> GetTestUser() { return new List< ...