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 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 解题报告的更多相关文章
- 【LeetCode】653. Two Sum IV - Input is a BST 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 日期 题目地址:ht ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 【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; 完
- [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 ...
- 【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 ...
- 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 ...
随机推荐
- C++设计模式——桥接模式
问题描述 现在要去画一个图形,图形有长方形.圆形和扇形等等:而图形又可以加上不同的颜色,然后,我们就可以画出红色的长方形,绿色的长方形:红色的圆形,绿色的圆形等等.而这种图形的形状在变化,图形的颜色也 ...
- Charles 抓包的简单使用
1.准备工具: 软件 Charles 手机 随意哪个现代手机 2.基本配置 安装Charles的电脑和手机在同一个局域网下, 点击手机上的和电脑练得同一个局域网的名字进行配置,里面有个代理,选择手动, ...
- C# 微信开发-----微信会员卡(二)
主要说说如何使用微信的激活会员卡 如图: 点击激活会员卡时,要跳转到如下的图片: 要实现这个功能,首先我们在创建会员卡后就操作如下代码 #region 添加激活时的自定义字段 string custo ...
- 《剑指offer》丑数
本题来自<剑指offer> 反转链表 题目: 思路: C++ Code: Python Code: 总结:
- Matlab将多项式的系数设为0
符号运算时有些多项式的系数值接近于0,像这样 fun = 3.5753839759325595498222646101085e-49*x + 1.836709923159824231201150839 ...
- Scrapy Selectors 选择器
0. 1.参考 <用Python写网络爬虫>——2.2 三种网页抓取方法 re / lxml / BeautifulSoup 需要注意的是,lxml在内部实现中,实际上是将CSS选择器转 ...
- 西北地区打不开github的解决办法~
泱泱我大西北,github打不开,的确痛苦的. http://ipaddress.com/ip_lookup/ 先查github.com 可能存在打不开的情况~ 随便找一个web在线代理,就可以查到了 ...
- ajax 函数回调
var initTaxPriod = function (taxNo) { intitSearch(); $("#taxPeriod").html(""); t ...
- contos最小包安装完后一些准备
yum upgradeyum install net-toolsyum -y install wgetyum -y install vim-enhanced yum install gcc gcc-c ...
- ArrayList Vector
100000条数据时:测了4次,分别是9ms/13ms:8ms/6ms:8ms/6ms:8ms/6ms[其中/前为ArrayList数据,/后为Vector数据]1000000条数据时:测了4次,分别 ...