题目描述:

自己的提交:

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. Web开发常规调试方法与常见问题分析

    一.Web项目基本原理 现在的web项目大都已经前后端独立开发与部署. 前后端独立开发,一般是前端与后端通过web接口(常见的有RESTful与websocket)文档进行交流.前端开发人员先更具业务 ...

  2. 利用纯css写三角形,弧度箭头,吃豆人,气泡。放大镜,标签的源码

    1. 向上三角形

  3. Vue之自建管理后台(三)登录页面

    在做登录页面之前,我们必须得完成路由的设定... 按照之前的设计我们路由的文件夹是src/router 官方默认的index.js,如下: import Vue from 'vue' import R ...

  4. Ubuntu 18.04 切换使用Python3

    我安装的Ubuntu 默认的python是2.7.5 python -V 我参考网上照到的文章,如果需要默认python为 python3 python命令默认是 python 3 sudo cp / ...

  5. 了解linux web的监听工具

    zabbix cacti Nagios 本想安装的,但是安装需要一个 空的服务器,因为服务器已经有安装 LAMP,故没有去了解 尝试了 cacti ,因为本地环境版本问题,只能使用0.8.8a版本,并 ...

  6. bzoj1034题解

    [解题思路] 广义田忌赛马的贪心模型.如果当前实力最差的马比对手实力最差的马强,则匹配:如果当前实力最强的马比对手实力最强的马强,亦匹配:若上述两点均不成立,拿己方最差的马去匹配对手最强的马.复杂度O ...

  7. 如何将Canvas中内容保存为图片

    Bitmap bm = Bitmap.createBitmap(320, 480, Config.ARGB_8888); Canvas canvas = new Canvas(bm); Paint p ...

  8. Flex birdeye笔记

    1.将官网示例demo运行起来 新建Flex项目,直接将官网src下的demo拷贝到新建的项目的src下  .将官网example-binaries目录下的文件拷贝到新建项目的bin-debug下即可 ...

  9. HDU1556-Color the ball-前缀和/线段树/树状数组

    N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但 ...

  10. Markdown 语法大全

    1 强调 星号与下划线都可以,单是斜体,双是粗体,符号可跨行,符号可加空格 **一个人来到田纳西** __毫无疑问__ *我做的馅饼 是全天下* _最好吃的_ 效果: 一个人来到田纳西 毫无疑问 我做 ...