题目描述:

自己的提交:

  1. class Solution:
  2. def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -> bool:
  3. if not root1 :return
  4. root_copy = root2
  5. while root1 and root_copy:
  6. if root1.val + root_copy.val < target:
  7. root_copy = root_copy.right
  8. elif root1.val + root_copy.val > target:
  9. root_copy = root_copy.left
  10. else:return True
  11. if self.twoSumBSTs(root1.left,root2,target):
  12. return True
  13. if self.twoSumBSTs(root1.right,root2,target):
  14. return True
  15. return False

另:

  1. class Solution(object):
  2. def twoSumBSTs(self, root1, root2, target):
  3. ans1 = []
  4. def dfs1(node):
  5. if node:
  6. dfs1(node.left)
  7. ans1.append(node.val)
  8. dfs1(node.right)
  9. ans2 = []
  10. def dfs2(node):
  11. if node:
  12. dfs2(node.left)
  13. ans2.append(node.val)
  14. dfs2(node.right)
  15. dfs1(root1)
  16. dfs2(root2)
  17. seen = set(ans1)
  18. for x in ans2:
  19. if target-x in seen:
  20. return True
  21. return False

优化:

  1. class Solution:
  2. def twoSumBSTs(self, root1: TreeNode, root2: TreeNode, target: int) -> bool:
  3. def conv(root):
  4. if not root:
  5. return []
  6. else:
  7. return [root.val] + conv(root.left) + conv(root.right)
  8. r1 = conv(root1)
  9. r2 = conv(root2)
  10. r1 = set(r1)
  11. r2 = set(r2)
  12. for i in r1:
  13. if target - i in r2:
  14. return True
  15. 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. Vuex篇

    [Vuex篇] vuex源码你学会了吗? 我来免费教你!~ 1.vuex是什么?  Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的 ...

  2. Codeforces Round #536 E. Lunar New Year and Red Envelopes /// 贪心 记忆化搜索 multiset取最大项

    题目大意: 给定n m k:(1≤n≤1e5, 0≤m≤200, 1≤k≤1e5) 表示n个时间长度内 最多被打扰m次 k个红包 接下来k行描述红包 s t d w:(1≤s≤t≤d≤n , 1≤w≤ ...

  3. 2019-8-31-dotnet-通过-HttpClient-下载文件同时报告进度的方法

    title author date CreateTime categories dotnet 通过 HttpClient 下载文件同时报告进度的方法 lindexi 2019-08-31 16:55: ...

  4. 74HC595点亮8个LED灯

    一.原理介绍 595有两个寄存器,都是8位的,如下所示: 595是串入并出带有锁存功能移位寄存器,它的使用方法简单: - -  在正常使用时 /SCLR接高电平,/G接低电平. - -  从SER每输 ...

  5. 如何恢复误删的OneNote页面

    今天不小心把半个月的日记删掉了!(为了减少页面数量,每个月的日记会放在同一个页面上). 幸运的是OneNote有自动备份功能,喜极而泣. 操作方法来自微软支持 打开丢失了最近笔记的笔记本. 单击“文件 ...

  6. VMware1设备与主机共享网络的问题

    问题的提出: 最近需要用到VMware1设备来配置网络,顺便将VMware1设备与主机进行共享网络,这样master就能直接访问网络了,但是原本以为直接在wlan设备上选择网络共享就行了,但是却没法收 ...

  7. SQL索引操作

    1. 创建索引 create index 索引名 on 表名(列名); 2. 删除索引 drop index 索引名; 3. 创建组合索引 create index 索引名 on 表名(列名1,,列名 ...

  8. vue实现京东动态楼层效果

    页面效果如下 <template> <div> <h1>首页</h1> <section class="floor-nav" ...

  9. NX二次开发-UFUN获取块的参数UF_MODL_ask_block_parms

    NX11+VS2013 #include <uf.h> #include <uf_modl.h> #include <uf_ui.h> UF_initialize( ...

  10. NX二次开发-UF_MODL_create_bplane创建有界平面

    这里要注意一点,有界平面是body,不是face,以前我刚开始做项目的时候一直以为有界平面是face,后来发现不对.是body NX9+VS2012 #include <uf.h> #in ...