leetcode-第10周双周赛-5080-查找两颗二叉搜索树之和
题目描述:


自己的提交:
class Solution:
def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -> bool:
if not root1 :return
root_copy = root2
while root1 and root_copy:
if root1.val + root_copy.val < target:
root_copy = root_copy.right
elif root1.val + root_copy.val > target:
root_copy = root_copy.left
else:return True
if self.twoSumBSTs(root1.left,root2,target):
return True
if self.twoSumBSTs(root1.right,root2,target):
return True
return False
另:
class Solution(object):
def twoSumBSTs(self, root1, root2, target):
ans1 = []
def dfs1(node):
if node:
dfs1(node.left)
ans1.append(node.val)
dfs1(node.right)
ans2 = []
def dfs2(node):
if node:
dfs2(node.left)
ans2.append(node.val)
dfs2(node.right)
dfs1(root1)
dfs2(root2)
seen = set(ans1)
for x in ans2:
if target-x in seen:
return True
return False
优化:
class Solution:
def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -> bool:
def conv(root):
if not root:
return []
else:
return [root.val] + conv(root.left) + conv(root.right)
r1 = conv(root1)
r2 = conv(root2)
r1 = set(r1)
r2 = set(r2)
for i in r1:
if target - i in r2:
return True
return False
leetcode-第10周双周赛-5080-查找两颗二叉搜索树之和的更多相关文章
- LeetCode 501. Find Mode in Binary Search Tree (找到二叉搜索树的众数)
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred ...
- [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最近公共祖先
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- 查找树ADT——二叉搜索树
在以下讨论中,虽然任意复杂的关键字都是允许的,但为了简单起见,假设它们都是整数,并且所有的关键字是互异的. 总概 使二叉树成为二叉查找树的性质是,对于树中的每个节点X,它的左子树中所有关键字值小于 ...
- LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)
题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...
- [leetcode]108. Convert Sorted Array to Binary Search Tree构建二叉搜索树
构建二叉搜索树 /* 利用二叉搜索树的特点:根节点是中间的数 每次找到中间数,左右子树递归子数组 */ public TreeNode sortedArrayToBST(int[] nums) { r ...
- LeetCode——1305. 两棵二叉搜索树中的所有元素
给你 root1 和 root2 这两棵二叉搜索树. 请你返回一个列表,其中包含 两棵树 中的所有整数并按 升序 排序.. 示例 1: 输入:root1 = [2,1,4], root2 = [1,0 ...
随机推荐
- python输入一个\输出2个\问题
在Python里面,如果\后面不是一个合法的转移字符,那么,Python会打印两个\,换句话说,Python将\也当成普通字符看待,而不是转义符的标志: >>>S = 'C:\py\ ...
- 项目中UX设计1到2的设计提升总结
- workman
workman安装: 环境安装: 检测依赖环境是否安装 rpm -qa | grep “软件或者包的名字”或者 yum list installed eg: rpm -qa | grep libeve ...
- keras 或 tensorflow 调用GPU报错:Blas GEMM launch failed
GPU版的tensorflow在模型训练时遇到Blas GEMM launch failed错误,或者keras遇到相同错误(keras 一般将tensorflow作为backend,如果安装了GPU ...
- RabbitMQ学习第四记:路由模式(direct)
1.什么是路由模式(direct) 路由模式是在使用交换机的同时,生产者指定路由发送数据,消费者绑定路由接受数据.与发布/订阅模式不同的是,发布/订阅模式只要是绑定了交换机的队列都会收到生产者向交换机 ...
- js 数组 常用方法
let arr=[{a:1},{a:2},{a:3}];//forEach 数组循环// 返回void arr.forEach(i=>{ // i为arr中的元素 }) //filter 数组过 ...
- GitHub不能访问问题
在C:\Windows\System32\drivers\etc文件夹下HOSTS里面最后添加: 192.30.253.112 github.com 192.30.253.113 github.com ...
- CSIC_716_20191129【面向对象高级----反射、类的内置方法(魔法方法)、单例模式】
反射 反射是通过'字符串'对 对象的属性进行操作,反射有四个内置的方法. hasattr 通过字符串 判断对象的属性或者方法是否存在 getattr 通过字符串 获取对象的属性或者方法 ...
- python-面向对象-01课堂笔记
面向对象 ''''1.面向过程编程 核心是"过程"二字,过程指的是解决问题的步骤,即先干什么再干什么 基于该思想编写程序就好比在编写一条流水线,是一种机械式的思维方式 ...
- .net 项目如何添加log4net日志记录
1.在项目根目录新建文件log4net.config,此文件中的节点解释还请自动百度. 文件实例: <?xml version="1.0"?><configura ...