File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Leonidas Savvides
Ranch Hand
Posts: 403
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For difficult recurenses like below, exist any trick to understand their result?

 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// Exercise 17.19 Solution: Tree.java

correct exe number...above
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33670
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic