programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# For difficult recurenses like below, exist any trick to understand their result

Leonidas Savvides
Ranch Hand
Posts: 403
For difficult recurenses like below, exist any trick to understand their result?

Ireneusz Kordal
Ranch Hand
Posts: 423
Hi,

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.

Leonidas Savvides
Ranch Hand
Posts: 403
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?

Leonidas Savvides
Ranch Hand
Posts: 403
// Exercise 17.19 Solution: Tree.java

correct exe number...above

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37465
539
Leonidas Savvides wrote:BTW what kinds of files may attached in forum?

Only images.

I think the way to look at recursion is to make sure you can find the recursive and base cases. Then look for how you get from one to another. Tracing the code often helps with that.