-
Notifications
You must be signed in to change notification settings - Fork 17
/
all-elements-in-two-binary-search-trees.py
44 lines (33 loc) · 1.22 KB
/
all-elements-in-two-binary-search-trees.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
38
39
40
41
42
43
44
from typing import List, Iterator, Optional
# 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]:
def dfs(node: TreeNode) -> Iterator[TreeNode]:
if not node:
return
yield from dfs(node.left)
yield node
yield from dfs(node.right)
def next_node(iterator: Iterator[TreeNode]) -> Optional[TreeNode]:
try:
return next(iterator)
except:
return None
left_iterator = dfs(root1)
left_node = next_node(left_iterator)
right_iterator = dfs(root2)
right_node = next_node(right_iterator)
result = []
while left_node or right_node:
if not right_node or (left_node and left_node.val < right_node.val):
result.append(left_node.val)
left_node = next_node(left_iterator)
else:
result.append(right_node.val)
right_node = next_node(right_iterator)
return result