File tree 3 files changed +47
-4
lines changed
3 files changed +47
-4
lines changed Original file line number Diff line number Diff line change 1
1
# LeetCode Algorithms
2
2
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 )
6
6
[ ![ PRs Welcome] ( https://img.shields.io/badge/PRs-welcome-brightgreen.svg )] ( CONTRIBUTING.md )
7
7
8
8
🔒 = Subscription Content
168
168
| 637 | [ Average of Levels in Binary Tree] ( https://leetcode.com/problems/average-of-levels-in-binary-tree ) | |
169
169
| 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 ) |
170
170
| 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 ) |
172
172
| 657 | [ Robot Return to Origin] ( https://leetcode.com/problems/robot-return-to-origin ) | |
173
173
| 661 | [ Image Smoother] ( https://leetcode.com/problems/image-smoother ) | |
174
174
| 665 | [ Non Deceasing Array] ( https://leetcode.com/problems/non-decreasing-array ) | |
Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments