Skip to content

Instantly share code, notes, and snippets.

@tamlt2704
Last active December 31, 2017 11:32
Show Gist options
  • Select an option

  • Save tamlt2704/8f2a282e148c5319504003df84c35dd9 to your computer and use it in GitHub Desktop.

Select an option

Save tamlt2704/8f2a282e148c5319504003df84c35dd9 to your computer and use it in GitHub Desktop.
# link: https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def findTarget(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: bool
"""
if not root: return False
bfs, s = [root], set()
for i in bfs:
if k - i.val in s: return True
s.add(i.val)
if i.left: bfs.append(i.left)
if i.right: bfs.append(i.right)
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment