leetcode1305 All Elements in Two Binary Search Trees
"""
Given two binary search trees root1 and root2.
Return a list containing all the integers from both trees sorted in ascending order.
Example 1:
Input: root1 = [2,1,4], root2 = [1,0,3]
Output: [0,1,1,2,3,4]
Example 2:
Input: root1 = [0,-10,10], root2 = [5,1,7,0,2]
Output: [-10,0,0,1,2,5,7,10]
Example 3:
Input: root1 = [], root2 = [5,1,7,0,2]
Output: [0,1,2,5,7]
Example 4:
Input: root1 = [0,-10,10], root2 = []
Output: [-10,0,10]
Example 5:
Input: root1 = [1,null,8], root2 = [8,1]
Output: [1,1,8,8]
"""
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None """
此题提供三种思路,与leetcode98类似,https://www.cnblogs.com/yawenw/p/12376942.html
第一种是层次遍历,
然后对存入的值sorted排序
传送门:https://blog.csdn.net/qq_17550379/article/details/103838538
"""
class Solution1:
def getAllElements(self, root1, root2):
q, res = [root1, root2], []
while q:
cur = q.pop(0)
if cur:
res.append(cur.val)
if cur.left != None:
q.append(cur.left)
if cur.right != None:
q.append(cur.right)
return sorted(res) """
第二种是利用二叉搜索树的条件,
对两个树分别中序遍历。这样两个list分别有序
再进行归并排序
"""
class Solution2:
def getAllElements(self, root1, root2):
q1, q2 = [], []
res = []
# 中序遍历
def inorder(root, q):
if root:
inorder(root.left, q)
q.append(root.val)
inorder(root.right, q)
inorder(root1, q1)
inorder(root2, q2)
# 归并排序的方法
while q1 or q2:
if not q1:
res += q2
break
if not q2:
res += q1
break
else:
res.append(q1.pop(0) if q1[0] < q2[0] else q2.pop(0))
return res """
第三种是:
先中序遍历(代替层次遍历)
再sorted(代替归并排序)
"""
class Solution3:
def getAllElements(self, root1, root2):
res = []
def inOrder(root):
if root: #!!!bug 没有写次if语句
inOrder(root.left)
res.append(root.val)
inOrder(root.right)
inOrder(root1)
inOrder(root2)
return sorted(res)
leetcode1305 All Elements in Two Binary Search Trees的更多相关文章
- Print Common Nodes in Two Binary Search Trees
Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two B ...
- [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 2 Unique Binary Search Trees II_Leetcode
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 【leetcode】Unique Binary Search Trees (#96)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LEETCODE —— Unique Binary Search Trees [动态规划]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 【LeetCode】95. Unique Binary Search Trees II
Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...
- Leetcode 86. Unique Binary Search Trees
本题利用BST的特性来用DP求解.由于BST的性质,所以root左子树的node全部<root.而右子树的node全部>root. 左子树 = [1, j-1], root = j, 右子 ...
- 【leetcode】Unique Binary Search Trees
Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...
随机推荐
- P1025数的划分
P1025数的划分 #include <iostream> using namespace std; int n,k; int cnt; void dfs(int s,int step,i ...
- Ngnix调整
1.隐藏版本号,防止针对版本攻击 http { server_tokens off;2.增加并发连接 2.1 worker_processes :改为CPU核数一致,因为异步IO进程是单 ...
- 十 Spring的AOP的底层实现:JDK动态代理和Cglib动态代理
SpringAOP底层的实现原理: JDK动态代理:只能对实现了接口的类产生代理.(实现接口默认JDK动态代理,底层自动切换) Cglib动态代理(类似Javassist第三方的代理技术):对没有实现 ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- axios发送post请求[body-parser]--['Content-type': 'application/x-www-form-urlencoded']
const express = require('express') const axios = require('axios') const bodyParser = require('body-p ...
- 定位(left 、right 、top 、 bottom)、padding、margin 值设为百分比值时
定位(left .right .top . bottom): top 为例 right 为例 padding.margin : 当padding.margin 值设为百分比值时,其百分比会相对于父元素 ...
- C语言笔记 13_排序算法
排序算法 冒泡排序 冒泡排序(英语:Bubble Sort)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序(如从大到小.首字母从A到Z)错误就把他们交换过来. 过程 ...
- nrm 源管理器
什么是nrm nrm 是一个 npm 源管理器,允许你快速地在 npm 源间切换. 安装nrm 在命令行执行命令,npm install -g nrm,全局安装nrm. 使用 执行命令nrm ls查看 ...
- C#中集合接口关系笔记
IEnumerable IEnumerable接口是所有集合类型的祖宗接口,其作用相当于Object类型之于其它类型.如果某个类型实现了IEnumerable接口,就意味着它可以被迭代访问,也就可以称 ...
- Vue 中引入echarts
安装依赖 npm install echarts -S 或者使用淘宝的镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org ...