There is a logic error in this code.
A depth of null node should be 0, but a depth of not null node that has both descendants null should be 1.
The other problem is that if you pass null as argument to this function, it will throw NullPointerException.
Try this version:
I think there is no any magic trick - you must play a while with recursion, and you'll get in the end.
Look and play with examples of code for calculating a factorial (a classic recursion example), fibonaci, hanoi.
Joined: Jan 31, 2010
I did the simples fibonaci, hanoi. I just get confused easily...
please note that this is from
deitel java how to program 7/E, and works you may find code online or request to submit it me... calculate binary tree level depth
// Exercise 17.25 Solution: Tree2.java // Chapter 17 Data Structures
BTW what kinds of files may attached in forum?