Skip to content

Commit 20a9acb

Browse files
feat: add python solution to lc problem: No.0653. Two Sum IV - Input is a BST (doocs#530)
* Added Python Solution to No.0653 in README.md * Added Python Solution to No.0653 in README_EN.md * feat: add Python solution to lc problems: No.0653 * Update Solution.py * Update README_EN.md * Update README.md Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent 5797fc2 commit 20a9acb

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

solution/0600-0699/0653.Two Sum IV - Input is a BST/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,24 @@ Target = 28
5454
<!-- 这里可写当前语言的特殊实现逻辑 -->
5555

5656
```python
57-
57+
# Definition for a binary tree node.
58+
# class TreeNode:
59+
# def __init__(self, val=0, left=None, right=None):
60+
# self.val = val
61+
# self.left = left
62+
# self.right = right
63+
class Solution:
64+
def findTarget(self, root: TreeNode, k: int) -> bool:
65+
def find(node):
66+
if not node:
67+
return False
68+
if k - node.val in nodes:
69+
return True
70+
nodes.add(node.val)
71+
return find(node.left) or find(node.right)
72+
73+
nodes = set()
74+
return find(root)
5875
```
5976

6077
### **Java**

solution/0600-0699/0653.Two Sum IV - Input is a BST/README_EN.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,24 @@
6060
### **Python3**
6161

6262
```python
63-
63+
# Definition for a binary tree node.
64+
# class TreeNode:
65+
# def __init__(self, val=0, left=None, right=None):
66+
# self.val = val
67+
# self.left = left
68+
# self.right = right
69+
class Solution:
70+
def findTarget(self, root: TreeNode, k: int) -> bool:
71+
def find(node):
72+
if not node:
73+
return False
74+
if k - node.val in nodes:
75+
return True
76+
nodes.add(node.val)
77+
return find(node.left) or find(node.right)
78+
79+
nodes = set()
80+
return find(root)
6481
```
6582

6683
### **Java**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 findTarget(self, root: TreeNode, k: int) -> bool:
9+
def find(node):
10+
if not node:
11+
return False
12+
if k - node.val in nodes:
13+
return True
14+
nodes.add(node.val)
15+
return find(node.left) or find(node.right)
16+
17+
nodes = set()
18+
return find(root)

0 commit comments

Comments
 (0)