The Collect Coins in a Tree coding problem is a complex graph problem. You are given a tree where some nodes contain coins. You start at any node and must collect all the coins and return to any node (not necessarily the start). The catch: you can collect a coin if you are within a distance of 2 from the node containing it. You want to find the minimum number of edges you must traverse.
This is a "Hard" problem asked by companies like Uber and Meta. It tests your ability to simplify a complex tree structure by "pruning." It requires identifying that certain parts of the tree (leaves with no coins) are useless, and other parts (nodes near coins) allow you to avoid traversing the entire tree. It evaluates high-level algorithmic thinking and graph reduction techniques.
The optimal Tree interview pattern involves Iterative Pruning:
Imagine a long line of nodes 1-2-3-4-5-6-7 where only 1 and 7 have coins.
1 and 7. New tree: 2-3-4-5-6.2 and 6. New tree: 3-4-5.(3,4) and (4,5). Total 2 edges.2 edges * 2 = 4.
Wait, if the tree becomes a single node or empty, the answer is 0.Pruning is a powerful technique for tree problems. Practice thinking about "What parts of the data can I safely ignore?" before writing any traversal code.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Sequence Reconstruction | Medium | Solve | |
| Longest Path With Different Adjacent Characters | Hard | Solve | |
| Build a Matrix With Conditions | Hard | Solve | |
| Minimum Edge Weight Equilibrium Queries in a Tree | Hard | Solve | |
| Parallel Courses III | Hard | Solve |