A long chain breaks recursion
transcript
Your height function works on every test. It runs fine on trees of a hundred nodes. Then a user has a long chain. It crashes with a recursion error.
A chain of seven nodes. Each call waits for the call below it. At the bottom, seven calls are waiting. A thousand nodes need a thousand calls. Python stops at about a thousand.
A balanced tree of a million nodes needs twenty waiting calls. Python allows about a thousand nested calls. A chain of n nodes needs n waiting calls. Other languages allow more, but not without limit.
List the nodes level by level from the top. Go through the list backwards. Each node follows its children, so no calls wait. The depth of the tree no longer matters.
Waiting calls equal the height. A chain has height n. A loop keeps its own list, not the call stack. Full breakdown in the lecture.