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 ...
随机推荐
- linux 服务器优化 --TIME_WAIT 问题
linux 服务器优化 --TIME_WAIT 问题: 问题现象: 1.外部机器不能正常连接SSH 2.内向外不能够正常的ping通过,域名也不能正常解析. 通过一些命令,查看服务器TIME_WAIT ...
- springboot不能加载主类
参考:https://www.cnblogs.com/iyyy/p/8085719.html 若为maven项目:用maven更新下项目.
- electron 查看版本信息
console.log(process) console.log(process.versions.electron) process 里包含很多信息:
- STM32单片机的软件重启和远程重启
STM32单片机可以通过以下代码实现重启(core_cm3.h).同时如果利用AT命令进行无线通讯,服务器后台和客户端之间用MODBUS通讯即4G+MODBUS RTU,可以利用F05写单个线圈的方法 ...
- Codeforces Round #580 (Div. 2)D(思维,Floyd暴力最小环)
#define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;const int maxn=300;cons ...
- 如何申请自己的Flag Counter?
申请自己的Flag Counter是一件简单的事. 首先,进入Flag Counter主页. 之后,选择好样式,点击"GET YOUR FLAG COUNTER".会弹出一个对话窗 ...
- Java连载80-数字类格式、随机数、BigDecimal
一.数字类 1.关于数字格式化:java.text.DecimalFormat; 2.数字格式元素: # 任意数字 , 千分位 . 小数点 0 不够补零 package com.bjpowernode ...
- 8.Memcache
1.概述 (1) Memcached是什么 Memcached是一款开源的.高性能的.分布式的内存对象缓存系统 (2) Memcached能干什么 最主要的功能就是:在内存中缓存数据,以减轻数据库负载 ...
- sklearn中调用集成学习算法
1.集成学习是指对于同一个基础数据集使用不同的机器学习算法进行训练,最后结合不同的算法给出的意见进行决策,这个方法兼顾了许多算法的"意见",比较全面,因此在机器学习领域也使用地非常 ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:基本的表格
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...