forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
37 lines (36 loc) · 1018 Bytes
/
Solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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: Optional[TreeNode], root2: Optional[TreeNode]
) -> List[int]:
def dfs(root: Optional[TreeNode], nums: List[int]) -> int:
if root is None:
return
dfs(root.left, nums)
nums.append(root.val)
dfs(root.right, nums)
a, b = [], []
dfs(root1, a)
dfs(root2, b)
m, n = len(a), len(b)
i = j = 0
ans = []
while i < m and j < n:
if a[i] <= b[j]:
ans.append(a[i])
i += 1
else:
ans.append(b[j])
j += 1
while i < m:
ans.append(a[i])
i += 1
while j < n:
ans.append(b[j])
j += 1
return ans