Я работал над вышеуказанной проблемой, Мое решение верное, но получаю TLE.
Ссылка на проблему: https://www.interviewbit.com/problems/k-distance/
class Solution:
# @param A : root node of tree
# @param B : integer
# @return an integer
def solve(self, A, B):
have={}
count=0
k=B
def recur(node):
nonlocal have
nonlocal count
nonlocal k
if node!=None:
for key in have:
if abs(key-node.val)<=k:
count+=have[key]
have[node.val]=have.get(node.val,0)+1
recur(node.left)
recur(node.right)
have[node.val]-=1
recur(A)
return count
Я что-то не так делаю ??