"""
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. 小程序云开发使用where查询遇到的问题

    想用小程序云开发的where查询,结果不论输入什么都是不报错,开始没注意,后来发现输入数据库中有的数据时,给打印出来查询成功,输入数据库中没有的数据时,也会得到一个集合,只不过这个集合的长度为0而已. ...

  2. tomcat中servlet冲突问题

    在启动tomcat以后,控制台发现“Offending class: javax/servlet/Servlet.class”信息: 信息: validateJarFile(E:\code\MyApp ...

  3. Atcoder Beginning Contest 134E(二分查找(upper_bound),思维)

    #include<bits/stdc++.h>using namespace std;int a[100007],f[100007],ans,n;int main(){ cin>&g ...

  4. WebView 设置请求头 Header

    package com.webview.demo; 2 3 import android.os.Bundle; 4 import android.support.v7.app.AppCompatAct ...

  5. C++中的四种类型转换运算符static_cast、dynamic_cast、const_cast和reinterpret_cast的使用

    1.上一遍讲述了C语言的隐式类型转换和显示类型转换,C语言之所以增加强制类型转换,就是为了强调转换的风险性,但这种强调风险的方式是比较粗放了,粒度比较大,它并没有表明存在什么风险,风险程度如何. 2. ...

  6. Go语言中的数组与数组切片

    Go中的数组与C的数组一样,只是定义方法不同 c: int a[10][10] Go [10][10]int 定义并初始化 array1 := [5]int{1,2,3,4,5} 变量名 := [in ...

  7. Guava LoadingCache不能缓存null值

    测试的时候发现项目中的LoadingCache没有刷新,但是明明调用了refresh方法了.后来发现LoadingCache是不支持缓存null值的,如果load回调方法返回null,则在get的时候 ...

  8. UGUI 特效怎样在UI上裁剪

    刚好碰到有人问怎样把粒子特效放到 UI 上并且能正确被 Mask 裁剪, 首先想到给粒子效果的 Shader 添加模板模仿一般 UI 的模板方式, 应该就能正确裁剪了吧, 不过没那么简单, 我们看到在 ...

  9. Random Variables

    可测空间(Measurable Space)和测度空间(Measure Space) 集合X,X上的一个σ-algebra A,则(X,A)被称为可测空间(measurable space) 再在A上 ...

  10. [Database] MAC MySQL中文乱码问题

    1 确保数据库编码设置, 可修改my.cnf mysql> show variables like '%character%'; +--------------------------+---- ...