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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def findTarget(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: bool
"""
if root:
q=[root]
rem=[]
while q:
qtopnode=q.pop(0)
if k-qtopnode.val in rem:
return True
rem.append(qtopnode.val)
if qtopnode.left:
q.append(qtopnode.left)
if qtopnode.right:
q.append(qtopnode.right) return False
return False

  

[LeetCode&Python] Problem 653. Two Sum IV - Input is a BST的更多相关文章

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

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

  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 解题报告(Python)

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

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

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

  7. [LeetCode&Python] Problem 167. Two Sum II - Input array is sorted

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

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

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

随机推荐

  1. Python3各种进制之间的转换方法

    一.2/8/10/16进制互转 1.1 2/8/10/16进制赋值 # 二进制赋值以0b打头 a = 0b1000 # 八进制赋值以0o打头,第一个是数字0第二个是字母o b = 0o1100 # 十 ...

  2. “SecureCRT遇到一个致命的错误且必须关闭”处理办法

    打开SecureCRT时报错:SecureCRT遇到一个致命的错误且发须关闭.一个崩溃转储文件已创建于... 解决办法是,如下在cmd中输入regedit回车打开注册表编缉器 展开HKEY_LOCAL ...

  3. 运维相关 docker

  4. java多线程读一个变量需要加锁吗?

    如果只是读操作,没有写操作,则可以不用加锁,此种情形下,建议变量加上final关键字: 如果有写操作,但是变量的写操作跟当前的值无关联,且与其他的变量也无关联,则可考虑变量加上volatile关键字, ...

  5. 各种形式的熵函数,KL距离

    自信息量I(x)=-log(p(x)),其他依次类推. 离散变量x的熵H(x)=E(I(x))=-$\sum\limits_{x}{p(x)lnp(x)}$ 连续变量x的微分熵H(x)=E(I(x)) ...

  6. 线性回归之决定系数(coefficient of determination)

    1. Sum Of Squares Due To Error 对于第i个观察点, 真实数据的Yi与估算出来的Yi-head的之间的差称为第i个residual, SSE 就是所有观察点的residua ...

  7. spark使用正则表达式读入多个文件

    String dir = "s3a://example/";String currentDir = dir + "{1[5-9],2[01]}/*.txt";J ...

  8. .NetCore发布到Centos docker

    将.netcore mvc项目发布到centos7的docker中.环境 vmware14+Centos7+docker-ce 1.使用vs将.netcoremvc项目发布到本地,修改发布后的目录 名 ...

  9. awk 中 fieldwidths使用方法

    AWK中的FIELDWIDTHS是一个很好用的变量,这个变量可以指定字符串按照怎么样的宽度进行展示 实例一: 要求: 032130 032131 146230 035048 222049 095070 ...

  10. ACM-ICPC 2018 徐州赛区网络预赛 G. Trace

    There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx , yy  ...