"""
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的更多相关文章

  1. 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 ...

  2. [LeetCode] Unique Binary Search Trees 独一无二的二叉搜索树

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. [LeetCode] Unique Binary Search Trees II 独一无二的二叉搜索树之二

    Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...

  4. 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 ...

  5. 【leetcode】Unique Binary Search Trees (#96)

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  6. LEETCODE —— Unique Binary Search Trees [动态规划]

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  7. 【LeetCode】95. Unique Binary Search Trees II

    Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) ...

  8. Leetcode 86. Unique Binary Search Trees

    本题利用BST的特性来用DP求解.由于BST的性质,所以root左子树的node全部<root.而右子树的node全部>root. 左子树 = [1, j-1], root = j, 右子 ...

  9. 【leetcode】Unique Binary Search Trees

    Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees) that st ...

随机推荐

  1. python 基础文件操作

    实时刷新到硬盘里 f= open('hh','w',encoding='utf8') f.write('gyftyftft') f.write('hghgh\njkkjk') f.flush()#实时 ...

  2. 【剑指Offer面试编程题】题目1506:求1+2+3+...+n--九度OJ

    题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 输入: 输入可能包含多个测试样例. 对于每个 ...

  3. sparkRDD:第1节 RDD概述;第2节 创建RDD

    RDD的特点: (1)rdd是数据集: (2)rdd是编程模型:因为rdd有很多数据计算方法如map,flatMap,reduceByKey等: (3)rdd相互之间有依赖关系: (4)rdd是可以分 ...

  4. highcharts Ajax 动态请求加载数据

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  5. node.js的C++入门

    最近的任务是把计划库的API用JavaScript语言调用起来,需要用Node.js的C++扩展,本文简单归总一下node.js addons官方文档https://nodejs.org/api/ad ...

  6. Vue和vue-template-compiler版本不一致

    vue项目,package.json中Vue和vue-template-compiler版本不一致时,执行npm run dev有时会报错, 提示vue和vue-template-compiler版本 ...

  7. redhat 7.6 find 命令

    1.按名字查找 find  ./    -name  filename    //精确查找 ,./ 代表当前目录   -name 查询名称 filename具体文件名称 find  ./    -na ...

  8. 「HNOI2003」消防局的设立

    题目 [内存限制:$256 MiB$][时间限制:$1000 ms$] [标准输入输出][题目类型:传统][评测方式:文本比较] 题目描述 2020 年,人类在火星上建立了一个庞大的基地群,总共有 $ ...

  9. 关于HTTP 协议

    HTTP简介 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的传送 ...

  10. container-coding-codec

    1 数字容器格式 container format 1.1 一些音频专有的容器: 1.2 静态图像专用的容器: 1.3 视频容器,可以容纳多种类型的音频和视频以及其他媒体 1.4 视频容器格式概述 1 ...