题目描述:

自己的提交:

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. leetcode.数组.287寻找重复数-Java

    1. 具体题目 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数,找出这个重复的数. 示例 1: ...

  2. leetcode.字符串.12整数转罗马数字-Java

    1. 具体题目 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. I 1V   5X 10L     50C    100D    500M   1000例如, 罗马数字 2 写做  ...

  3. 【VUE/JS】vue和js禁止浏览器页面后退

    1.vue 禁止浏览器后退需求是:需要某个路由不能通过浏览器返回,同时不影响相互之间的切换整理一下解决方法 和 使用方法: 1.在路由配置中给这个路由添加meta信息,比如: { path: '/ho ...

  4. python项目部署

    WSGI简介 Web框架和Wen服务器之间需要进行通信,如果在设计时它们之间无法相互匹配,那么对框架的选择就会限制对Web服务器的选择,这显然是不合理的.这时候需要设计一套双方都遵守的接口.WSGI是 ...

  5. Kettle使用kettle.properties

    kettle.properties 是一个变量文件,这个文件我使用的最多的地方是保存 “数据库连接” 用户名和密码. 如果不用这个文件,那么使用“数据库连接”时,需要硬编码写到文件里. 有一天dba告 ...

  6. 《提高c++性能的编程技术》读书笔记

    一个程序的执行效率是取决于改程序翻译成汇编语言之后的执行的机器指令的条数.而每一个机器指令的执行的周期是一定的.C语言和C++都是高于汇编语言的高级语言,其中,C语言源代码与其相应的机器指不是完全同一 ...

  7. Linux系统的buff/cache缓存清理脚本

    cacheClean.sh #!/bin/bash # 日期: # 作者: 黄慧丰/何鹏举 # 说明: fastdfs所在的linux系统的buffer cache过大, 且并没有有效的自动回收, 因 ...

  8. lterator遍历

    iterator是一种接口机制,为各种不同的数据结构提供统一的访问机制 作用: 1.为各种数据结构,提供一个统一的.简便的访问接口: 2.使得数据结构的成员能够按某种次序排列 3.ES6创造了一种新的 ...

  9. EF批量添加数据之修改SQL Server执行上限

    asp.net core 项目 打开Startup.cs services.AddDbContext<MyContext>( options => { options.UseSqlS ...

  10. 9.RabbitMQ Topic类型交换机

    RabbitMQ消息服务中Topic类型交换机根据通配符路由消息,*代表一个单词,#代表代表0或多个单词.   生产者 消费者   代码 Producer.java   package com.tes ...