I'm wondering if anyone has any good explanations/articles/approaches to think about recursive functions which usually have variables which themselves are recursive functions... for example if we look at the diameter of binary tree question, we have the following solution which assigns a recursive function to a variable (this is a simple example, however, some examples get way more complicated and I feel like I always struggle with these questions)
def depth(root):
if not root: return 0
ansL = depth(root.left)
ansR = depth(root.right)
self.best = max(self.best, ansL + ansR + 1)
return 1 + max(ansL, ansR)If anyone has any good tricks or approaches, that would be greatly appreciated!!