Note that you're basically asking for a code review here -- which is a different site, StackExchange.CodeReview. However, a simple trace of calls might be within Stack Overflow's charter. Complete your MRE. Insert some basic diagnosis, such as counting function calls -- you should have
one call per node in the tree. Then you have a
specific question, and we can work on your problem. As it stands, you're leaving us to eye-check your code -- as the posting guidelines tell you, "Make it easy for others to help you." —
Prune 15 secs ago