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 ...
随机推荐
- 面试总结【css篇】- css选择器以及优先级
优先(优先级为): !important > 内联样式 > #id > .class > tag > * > 继承 > 默认 . 当选择器的权重相同时,它将 ...
- GetModuleHandleW 分析
首先查询MSDN,可以清楚地看到 位于kernel32 dll 里面. 有目标就好办,找到这个dll,然后,开工,进入IDA. 跳啊 就到下面那块了. 遗憾的是...显然不是这里阿,实际上下一块调用的 ...
- C预处理之宏定义
#include <stdio.h> //定义不带参数的宏 #define PI 3.14 /*********************************************** ...
- 函数中的toString
function Person(){ this.name = name; this.age = age; this . gender = gender; } // 创建一个Person实例 var ...
- 2018-2-13-win10-uwp-入门
title author date CreateTime categories win10 uwp 入门 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23 ...
- Mysql优化-典型的服务器配置
内存配置相关参数 mysql内存分配需要考虑到操作系统需要使用的内存,其他应用程序所要使用的内存,mysql的会话数以及每个会话使用的内存,然后就是操作系统实例所使用的内存.生产环境的mysql往往都 ...
- VBA+SQL transform pivot union联合查询的基础应用
Sub 项目状态查询() '如果“项目状态”是未转运营那么实施状态是不能选择的,因为还没有实施.'如果“项目状态”选择状态后,那么项目名称里面只显示该状态的项目名称.如果“项目状态”选择的不是未转运营 ...
- fedora 28 winscp链接不上
systemctl restart sshd.service //启动sshd服务 systemctl stop firewalld //关闭防火墙 /etc/selinux/config //关闭s ...
- HashMap和Hashtable有什么区别
HashMap和Hashtable都实现了Map接口,因此很多特性非常相似.但是,他们有以下不同点: HashMap允许键和值是null,而Hashtable不允许键或者值是null. Hashtab ...
- NX二次开发-UFUN获取工程图的数量和tag UF_DRAW_ask_drawings
NX9+VS2012 #include <uf.h> #include <uf_draw.h> #include <uf_part.h> #include < ...