Skip to content

Commit 2578ae8

Browse files
solves two sum iv
1 parent b3afefe commit 2578ae8

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

Diff for: README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# LeetCode Algorithms
22

3-
![problems-solved](https://img.shields.io/badge/Problems%20Solved-120/571-1f425f.svg)
4-
![problems-solved-java](https://img.shields.io/badge/Java-120/1571-1abc9c.svg)
5-
![problems-solved-python](https://img.shields.io/badge/Python-120/1571-1abc9c.svg)
3+
![problems-solved](https://img.shields.io/badge/Problems%20Solved-143/571-1f425f.svg)
4+
![problems-solved-java](https://img.shields.io/badge/Java-143/1571-1abc9c.svg)
5+
![problems-solved-python](https://img.shields.io/badge/Python-143/1571-1abc9c.svg)
66
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](CONTRIBUTING.md)
77

88
🔒 = Subscription Content
@@ -168,7 +168,7 @@
168168
| 637 | [Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree) | |
169169
| 643 | [Maximum Average SubArray I](https://leetcode.com/problems/maximum-average-subarray-i) | [![Java](assets/java.png)](src/MaximumAverageSubArrayI.java) [![Python](assets/python.png)](python/maximum_average_subarray_I.py) |
170170
| 645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch) | [![Java](assets/java.png)](src/SetMismatch.java) [![Python](assets/python.png)](python/set_mismatch.py) |
171-
| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst) | |
171+
| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst) | [![Java](assets/java.png)](src/TwoSumIVInputIsABST.java) [![Python](assets/python.png)](python/two_sum_iv.py) |
172172
| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin) | |
173173
| 661 | [Image Smoother](https://leetcode.com/problems/image-smoother) | |
174174
| 665 | [Non Deceasing Array](https://leetcode.com/problems/non-decreasing-array) | |

Diff for: python/two_sum_iv.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
8+
9+
class Solution:
10+
def __init__(self):
11+
self.elements = set()
12+
13+
def findTarget(self, root: TreeNode, k: int) -> bool:
14+
if root is None: return False
15+
if k - root.val in self.elements: return True
16+
self.elements.add(root.val)
17+
return self.findTarget(root.left, k) or self.findTarget(root.right, k)

Diff for: src/TwoSumIVInputIsABST.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class TreeNode {
5+
int val;
6+
TreeNode left;
7+
TreeNode right;
8+
TreeNode() {}
9+
TreeNode(int val) { this.val = val; }
10+
TreeNode(int val, TreeNode left, TreeNode right) {
11+
this.val = val;
12+
this.left = left;
13+
this.right = right;
14+
}
15+
}
16+
17+
public class TwoSumIVInputIsABST {
18+
Set<Integer> elements = new HashSet<>();
19+
20+
public boolean findTarget(TreeNode root, int k) {
21+
if (root == null) return false;
22+
if (elements.contains(k - root.val)) return true;
23+
elements.add(root.val);
24+
return findTarget(root.left, k) || findTarget(root.right, k);
25+
}
26+
}

0 commit comments

Comments
 (0)