前序遍历全部的BT,顺序插入结果数组中。Note:bisect.bisect_left, bisect.bisect_right, bisect.insort, bisect.insort_left, bisect.insort_right得牢固记忆。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def getAllElements(self, root1: TreeNode, root2: TreeNode) -> List[int]:
self.res = []
def traverse(root):
if root is None:return
bisect.insort_left(self.res, root.val)
traverse(root.left)
traverse(root.right)
traverse(root1)
traverse(root2)
return self.res