aspose file tools*
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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
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: 402
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: 402
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: 402
// Exercise 17.19 Solution: Tree.java

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

Joined: May 26, 2003
Posts: 29220
    
135

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
Trouble with a toy tree [SOLVED]
getting stackOverFlowError
Trouble with recursion
Binary Search Trees
BST problem (Online waiting)