Two Sum IV - Input is a BST (Leetcode 653) [Optimisation Required]
Problem Link: https://leetcode.com/problems/two-sum-iv-input-is-a-bst/
from collections import defaultdict
class Solution:
def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
global d
d=defaultdict(int)
def fun(root,k):
global d
if(root is None):
return False
if(k-root.val in d):
return True
else:
d[root.val]+=1
l=fun(root.left,k)
r=fun(root.right,k)
if(l or r):
return True
else:
return False
return fun(root,k)
PreviousSum of Left Leaves (Leetcode 404)NextKth Smallest Element in a BST (Leetcode 230) [Optimisation Required]
Last updated