Leetcode 653 Solution
This article provides solution to leetcode question 653 (two-sum-iv-input-is-a-bst)
Access this page by simply typing in "lcs 653" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/two-sum-iv-input-is-a-bst
Solution
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def findTarget(self, root: TreeNode, k: int) -> bool:
m = set()
q = [root]
m.add(root.val)
while q:
node = q.pop(0)
if node.left:
q.append(node.left)
m.add(node.left.val)
if k - node.left.val != node.left.val and k - node.left.val in m:
return True
if node.right:
q.append(node.right)
m.add(node.right.val)
if k - node.right.val != node.right.val and k - node.right.val in m:
return True
return False