【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, 力 ...
随机推荐
- 17.DRX操作时序
DRX状态转移 OnDuration:监听态,监听并接收PDCCH,也叫激活态 StayActive:保持激活状态(做完业务后,继续监听一段时间和一个非激活定时器时间,或者还有缓存) off:off状 ...
- [转帖][Linux]systemd和sysV
[Linux]systemd和sysV 转自:https://www.cnblogs.com/EasonJim/p/7168216.html 在Debian8中systemd和sysVinit同时 ...
- vmware中的虚拟linux配置多块网卡
在使用vm上运行多个linux系统,来模拟LVS负载均衡实验中.需要在lvs服务器中设置两块网卡,发现可以在vm给虚拟机添加任意多个网卡. 方法: 不要启动Linux,在上面的菜单项中选择: “VM— ...
- redis 小结三-数据类型
redis 的数据类型主要有五种 字符串(String) 哈希表(Hash) 列表(List) 集合(Set ) 有序集合(Sorted Set) 1. 字符串 一个 key 对应一个 value 该 ...
- mysql5.6
5.6 与之后版本有差别本文以5.6为例** 1.mysql5.6安装 本文采用2进制安装 mkdir /server/tools -p cd /server/tools 1.下载 wget http ...
- python变量的内存管理
python变量的内存管理 一.变量存在了哪里? 先让我们来看一段代码: height = 100 # 定义变量 # print(100) # print会自动帮你创建一个变量100,打印完之后,马上 ...
- vue-router的query和params的区别
vue-router的query和params的区别 首先简单来说明一下$router和$route的区别 $router为VueRouter实例,想要导航到不同url,则使用$router.push ...
- vue.js的v-bind
v-bind v-bind 主要用于属性绑定, html中的标签内: <div class="control-group"> <label class=&quo ...
- 06.AutoMapper 之内联映射(Inline Mapping)
https://www.jianshu.com/p/623655d7cb34 内联映射(Inline Mapping) AutoMapper在 6.2 以上版本将动态创建类型映射. 当第一次调用Map ...
- Filter实现登录功能限制
public void doFilter(ServletRequest arg0,ServletResponse arg1,FilterChain chain) throws IOException, ...



