"""
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. [蓝桥杯2017初赛]跳蚱蜢 BFS

    题目描述 如图所示: 有9只盘子,排成1个圆圈.其中8只盘子内装着8只蚱蜢,有一个是空盘. 我们把这些蚱蜢顺时针编号为 1~8.每只蚱蜢都可以跳到相邻的空盘中,也可以再用点力,越过一个相邻的蚱蜢跳到空 ...

  2. python打印日志log

    整理一个python打印日志的配置文件,是我喜欢的格式. # coding:utf-8 # 2019/11/7 09:19 # huihui # ref: import logging LOG_FOR ...

  3. [网络必学]TCP/IP四层模型讲解【笔记整理通俗易懂版】

    OSI七层模型     表示层:用来解码不同的格式为机器语言,以及其他功能. 会话层:判断是否需要网络传输. 传输层:识别端口来指定服务器,如指定80端口的www服务. 网络层:提供逻辑地址选路,即发 ...

  4. mybatis动态参数查询

    参考:https://blog.csdn.net/zbw18297786698/article/details/53727594

  5. Laradock 使用中遇到的问题汇总

    1.ErrorException] mkdir (): Permission denied 解决:权限不够,thinkphp5下,runtime 文件夹改777,文件所有者改为 laradock(进入 ...

  6. Linux命令:cp命令

    cp命令作用:拷贝文件和目录 一.格式 cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION].. ...

  7. bootstrap如何设置每一个选项卡对应一个页面

    bootstrap选项卡如果直接在每一个选项div中直接插入页面,可以使用<object type="text/html" data="test.html" ...

  8. 解决vmware 桥联 再次使用联不上网的问题

    在vmare里 编辑 虚拟网络配置   桥联  自动设置 改为你正在联网的网卡  这个问题针对有线网卡 和无限网卡使用的问题

  9. Lesson 48 Planning a share portfolio

    How does the older investor differ in his approach to investment from the younger investor? There is ...

  10. Eclipse设置自动提示代码(不用alt+/了)

    在preferences找到如图的相关位置.在输入框里把26个字母加进去,qwer...........