Skip to content

Commit 879d2ec

Browse files
committed
weekly
1 parent 010a59c commit 879d2ec

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

my-submissions/m1214.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def twoSumBSTs(self, root1: Optional[TreeNode], root2: Optional[TreeNode], target: int) -> bool:
9+
bst2 = set()
10+
nxt = [root2]
11+
while nxt :
12+
curr = nxt.pop()
13+
bst2.add(curr.val)
14+
if curr.left :
15+
nxt.append(curr.left)
16+
if curr.right :
17+
nxt.append(curr.right)
18+
19+
nxt = [root1]
20+
while nxt :
21+
curr = nxt.pop()
22+
if target - curr.val in bst2 :
23+
return True
24+
25+
if curr.left :
26+
nxt.append(curr.left)
27+
if curr.right :
28+
nxt.append(curr.right)
29+
30+
return False

0 commit comments

Comments
 (0)