Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6e3e427

Browse files
authored
Create Day-23_Two Sum IV - Input is a BST.py
1 parent c69dc21 commit 6e3e427

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# TC: O(N) | SC: O(N)
2+
3+
from collections import defaultdict
4+
class Solution:
5+
def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
6+
d = defaultdict(int)
7+
if root.left is None and root.right is None:
8+
return False
9+
def inorder(root, d):
10+
if root is None:
11+
return
12+
inorder(root.left, d)
13+
d[root.val] += 1
14+
inorder(root.right, d)
15+
16+
inorder(root, d)
17+
18+
for i in d.keys():
19+
val = k - i
20+
if val in d and val != i:
21+
return True
22+
23+
return False
24+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /