Leetcode #1443: Minimum Time to Collect All Apples in a Tree

Whenever you are at a node, say p, you will collect all apples in p’s subtree before returning back to the original root. This will avoid traveling the same path multiple times. …. Because you have the list of edges, construct a better representation – adjacency list – of the tree. … More Leetcode #1443: Minimum Time to Collect All Apples in a Tree


Recursion makes exploration of all paths/branches of the tree very easy. At any time you only have to think about the current level and recursively call the function for exploring the children. Whenever you have a recursive code, you should always think about the base case where you will terminate the recursion. Depending on the problem it would, of course, differ. For the above example, the terminating condition can be “Is the current node the key?” … More Backtracking


Formally, as you might already know, recursion is a process when a function calls itself directly or indirectly. When a function calls another function, we can easily imagine the execution going into the other function and coming back to the calling function once the called function is executed completely. It’s a “visual” process. … More Recursion