I need help finding the depth of a binary tree. I want to run this after each insert and print out the depth. Any help would be appreciated. Can anyone give me some sample code that would do this or tell me how to do this??
... which means it may or may not be what you want. If your tree is being built evenly, then the depth shouldn't vary by more than one whatever branch you follow. In which case you can follow a single branch and get the answer to within one, without any recursion or large-scale searching (which will happen if you search the whole tree recursively).
If the tree is not being built evenly, then the recursive version tells you the deepest bit.
Remember that a single insert can only change the depth by one. If you modify your insert function you can make it return the depth of insert. Otherwise every time you do the insert you will search the entire tree.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.