我正在处理上述问题,我的解决方案是正确的,但得到了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
我做错什么了吗
目前没有回答
相关问题 更多 >
编程相关推荐