This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes For difficult recurenses like below, exist any trick to understand their result Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "For difficult recurenses like below, exist any trick to understand their result" Watch "For difficult recurenses like below, exist any trick to understand their result" New topic
Author

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

Leonidas Savvides
Ranch Hand

Joined: Jan 31, 2010
Posts: 397
For difficult recurenses like below, exist any trick to understand their result?

Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
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

Joined: Jan 31, 2010
Posts: 397
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

Joined: Jan 31, 2010
Posts: 397
// Exercise 17.19 Solution: Tree.java

correct exe number...above
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26138
    
  66

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For difficult recurenses like below, exist any trick to understand their result
 
Similar Threads
Binary Search Trees
BST problem (Online waiting)
Trouble with recursion
Trouble with a toy tree [SOLVED]
getting stackOverFlowError