-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.py
30 lines (29 loc) · 875 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
# 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 twoSumBSTs(
self, root1: Optional[TreeNode], root2: Optional[TreeNode], target: int
) -> bool:
def dfs(root: Optional[TreeNode], i: int):
if root is None:
return
dfs(root.left, i)
nums[i].append(root.val)
dfs(root.right, i)
nums = [[], []]
dfs(root1, 0)
dfs(root2, 1)
i, j = 0, len(nums[1]) - 1
while i < len(nums[0]) and ~j:
x = nums[0][i] + nums[1][j]
if x == target:
return True
if x < target:
i += 1
else:
j -= 1
return False