The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: Finding deepest node of tree??

The code does not find the deepest node, but all leaf nodes - which based upon the tree posted above are 23 and 21. To find the deepest node, you need to get the depth/level as well, and find the node(s) with the highest value. Many ways to do this, possibly the easiest might be to use a recursive method which has the level value passed as a parameter, from which you can determine the 'deepest'

Re: Finding deepest node of tree??

Here is my code:
- First you need to find the depth/height of the tree.
- Then traverses through each node, find the current depth and compare it with the maxDepth.
- If condition satisfied, print out the item stored in the node.

Re: Finding deepest node of tree??

sorry for asking this noobish qn.. but is it possible to post some example codes?

phamminh91 has given some great pseudo-code to begin with. Recommend you give it a go, break the problem down as phamminh91 has outlined and do this step by step...I will note that you've posted a solution using a while loop, and you may find it easier to use a recursive approach. The following link provides some great descriptions and examples to tackle the problemBinary Trees

Re: Finding deepest node of tree??

Originally Posted by copeg

phamminh91 has given some great pseudo-code to begin with. Recommend you give it a go, break the problem down as phamminh91 has outlined and do this step by step...I will note that you've posted a solution using a while loop, and you may find it easier to use a recursive approach. The following link provides some great descriptions and examples to tackle the problemBinary Trees