【leetcode】1214.Two Sum BSTs
题目如下:
Given two binary search trees, return
Trueif and only if there is a node in the first tree and a node in the second tree whose values sum up to a given integertarget.Example 1:
Input: root1 = [2,1,4], root2 = [1,0,3], target = 5
Output: true
Explanation: 2 and 3 sum up to 5.Example 2:
Input: root1 = [0,-10,10], root2 = [5,1,7,0,2], target = 18
Output: falseNote:
- Each tree has at most
5000nodes.-10^9 <= target, node.val <= 10^9
解题思路:我用的是最直接的方法,把两棵树的节点的值分别存到两个字典中,然后遍历字典,看看能不能组成target。
代码如下:
# 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 twoSumBSTs(self, root1, root2, target):
"""
:type root1: TreeNode
:type root2: TreeNode
:type target: int
:rtype: bool
"""
dic1 = {}
dic2 = {}
def recursive(node,dic):
if node != None:
dic[node.val] = 1
if node.left != None:
recursive(node.left,dic)
if node.right != None:
recursive(node.right, dic)
recursive(root1,dic1)
recursive(root2,dic2)
for val in dic1.iterkeys():
if target - val in dic2:
return True
return False
【leetcode】1214.Two Sum BSTs的更多相关文章
- 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【LeetCode】167. Two Sum II - Input array is sorted
Difficulty:easy More:[目录]LeetCode Java实现 Description Given an array of integers that is already sor ...
- 【LeetCode】170. Two Sum III – Data structure design
Difficulty:easy More:[目录]LeetCode Java实现 Description Design and implement a TwoSum class. It should ...
- 【LeetCode】#1 Two Sum
[Question] Given an array of integers, return indices of the two numbers such that they add up to a ...
- 【LeetCode】1005. Maximize Sum Of Array After K Negations 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 小根堆 日期 题目地址:https://leetco ...
- 【LeetCode】938. Range Sum of BST 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】167. Two Sum II - Input array is sorted 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】1. Two Sum 两数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:two sum, 两数之和,题解,leetcode, 力 ...
随机推荐
- 意想不到的JavaScript(每日一题3)
题目: 答案: 1 4 3 2 解析:
- 图片下载&&非同源图片下载&&同源下载&&网页点击下载图片
非同源图片下载(html添加canvas标签) 方法1: downloadImgByBase64(url){ console.log() // 创建隐藏的可下载链接 // let blob = ...
- Linux 下文件句柄数的查询学习
1. 查看 所有进程的 打开的句柄数 lsof -n|awk '{print $2}'|sort|uniq -c |sort -nr|more 效果为: 2. 查看某一个进程内的 文件信息 lsof ...
- [转帖]Chrome 错误代码:ERR_UNSAFE_PORT
Chrome 错误代码:ERR_UNSAFE_PORT 2018年07月18日 09:07:50 孤舟听雨 阅读数 182 https://blog.csdn.net/u013043762/artic ...
- (5.1.5)引擎管理——多服务器管理之中央管理服务器(CMS)
关键词:中央管理服务器,CMS,多服务器管理 中央管理服务器 -[1]打开 视图->已注册的服务器 [2]注册中央管理服务器 右击中央管理器->注册中央管理服务器 这里输入IP.主机名都可 ...
- springboot - 应用实践(1)认识springboot
1.为什么要推出springboot springboot设计的目的是用来简化新spring应用的初始搭建以及开发过程.springboot遵循“约定优于配置”原则. 2.springboot默认的配 ...
- Tomcat配置:java.lang.UnsatisfiedLinkError: D:\DevelopTool\tool20150402\tomcat\apache-tomcat-8.5.16\bin\tcnative-1.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform
解决办法: tomcat启动时提示java.lang.UnsatisfiedLinkError: D:\soft\devTool\apache-tomcat-7.0.57\bin\tcnative-1 ...
- redis 命令都在这了
DEL key [key ...]删除指定的key(一个或多个) DUMP key导出key的值 EXISTS key [key ...]查询一个key是否存在 EXPIRE key seconds设 ...
- Python自学
print("\u4e2d\u56fd\") 报错,语法错误 修改,去掉尾部的\,正确 import datetimeprint("now:"+datetime ...
- 服务器配置好但Idea/Datagrip无法连接远程数据库的解决方案
服务器没有开放端口3306,在云服务控制台配置安全组即可.



