题目描述:

自己的提交:

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-查找两颗二叉搜索树之和的更多相关文章

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

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

  3. [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 ...

  4. [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 ...

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

  6. 查找树ADT——二叉搜索树

    在以下讨论中,虽然任意复杂的关键字都是允许的,但为了简单起见,假设它们都是整数,并且所有的关键字是互异的. 总概   使二叉树成为二叉查找树的性质是,对于树中的每个节点X,它的左子树中所有关键字值小于 ...

  7. LeetCode OJ :Unique Binary Search Trees II(唯一二叉搜索树)

    题目如下所示:返回的结果是一个Node的Vector: Given n, generate all structurally unique BST's (binary search trees) th ...

  8. [leetcode]108. Convert Sorted Array to Binary Search Tree构建二叉搜索树

    构建二叉搜索树 /* 利用二叉搜索树的特点:根节点是中间的数 每次找到中间数,左右子树递归子数组 */ public TreeNode sortedArrayToBST(int[] nums) { r ...

  9. LeetCode——1305. 两棵二叉搜索树中的所有元素

    给你 root1 和 root2 这两棵二叉搜索树. 请你返回一个列表,其中包含 两棵树 中的所有整数并按 升序 排序.. 示例 1: 输入:root1 = [2,1,4], root2 = [1,0 ...

随机推荐

  1. 全网最热Python3入门+进阶 更快上手实际开发✍✍✍

    全网最热Python3入门+进阶 更快上手实际开发  整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问 ...

  2. 在vue中获取不到canvas对象? 两种解决办法。

    1. mounted 钩子函数 初次肯定获取到id 2. 如果canvas父级用到了v-if 请改成v-show ,vue Dom节点 重新渲染导致methods 方法获取不到对象.

  3. 《代码大全2》读书笔记 Week3

    <代码大全2>第六.七章 作者在第六章中从抽象数据类型(Abstract Data Type)出发阐释类(class)的概念,给出创建类的原因以及创建高质量的常涉及的设计问题.抽象数据类型 ...

  4. 基于LNMP部署DiscuzX

    [root@nginx~]# unzip ComsenzDiscuz-DiscuzX-master.zip[root@nginxDiscuzX]# mv upload/ /usr/local/ngin ...

  5. Interview - 面试题汇总目录

    参考 java 入门面试题 https://blog.csdn.net/meism5/article/details/89021536 一.Java 基础 1.JDK 和 JRE 有什么区别? 2.= ...

  6. JUC 一 ConcurrentHashMap

    java.util.concurrent ConcurrentHashMap是一个支持并发检索和并发更新的线程安全的HashMap(但不允许空key或value). JDK8以CAS+synchron ...

  7. 在线暴躁:<script />问题

    这个问题是今天发现的,以前都没注意到这个问题: <script src="./vue/vue.min.js" /> <script src="./vue ...

  8. 视频质量评测标准——VMAF

    阿里云视频云直播转码每天都会处理大量的不同场景.不同编码格式的直播流.为了保证高画质,团队借助VMAF标准来对每路转码的效果做质量评估,然后进行反馈.调优.迭代.这么做的原因在于,像动作片.纪录片.动 ...

  9. 关于windows下远程连接Linux服务器的方法(CentOs)

    1.服务器端安装VNC 1) 安装vncserver yum install -y tigervnc-server 2) 修改配置 vi /etc/sysconfig/vncservers   最后两 ...

  10. PostgreSQL/GREENPLUM关联更新

    update a_t AA set /*AA.*/ sqlstr = 'qqq' from a_t BB where aa.id <> BB.id and aa.name = BB.nam ...