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 ...
随机推荐
- Vuex篇
[Vuex篇] vuex源码你学会了吗? 我来免费教你!~ 1.vuex是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的 ...
- 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≤ ...
- 2019-8-31-dotnet-通过-HttpClient-下载文件同时报告进度的方法
title author date CreateTime categories dotnet 通过 HttpClient 下载文件同时报告进度的方法 lindexi 2019-08-31 16:55: ...
- 74HC595点亮8个LED灯
一.原理介绍 595有两个寄存器,都是8位的,如下所示: 595是串入并出带有锁存功能移位寄存器,它的使用方法简单: - - 在正常使用时 /SCLR接高电平,/G接低电平. - - 从SER每输 ...
- 如何恢复误删的OneNote页面
今天不小心把半个月的日记删掉了!(为了减少页面数量,每个月的日记会放在同一个页面上). 幸运的是OneNote有自动备份功能,喜极而泣. 操作方法来自微软支持 打开丢失了最近笔记的笔记本. 单击“文件 ...
- VMware1设备与主机共享网络的问题
问题的提出: 最近需要用到VMware1设备来配置网络,顺便将VMware1设备与主机进行共享网络,这样master就能直接访问网络了,但是原本以为直接在wlan设备上选择网络共享就行了,但是却没法收 ...
- SQL索引操作
1. 创建索引 create index 索引名 on 表名(列名); 2. 删除索引 drop index 索引名; 3. 创建组合索引 create index 索引名 on 表名(列名1,,列名 ...
- vue实现京东动态楼层效果
页面效果如下 <template> <div> <h1>首页</h1> <section class="floor-nav" ...
- NX二次开发-UFUN获取块的参数UF_MODL_ask_block_parms
NX11+VS2013 #include <uf.h> #include <uf_modl.h> #include <uf_ui.h> UF_initialize( ...
- NX二次开发-UF_MODL_create_bplane创建有界平面
这里要注意一点,有界平面是body,不是face,以前我刚开始做项目的时候一直以为有界平面是face,后来发现不对.是body NX9+VS2012 #include <uf.h> #in ...