Reputation: 87
I am working on an assignment right now, and I have found a solution to the problem online (which looks ridiculously simple but works like magic)
I am still having some trouble comprehending how recursion exactly works and I actually want to learn.
Could someone help me with understanding this logic please?
The question is to find the longest path from root node to a leaf node. (Basically find the height of the tree ?).
The function is as follows:
public static int findPath(TreeNode<Integer> node)
{
if (node == null)
return 0;
else
{
return 1 + Math.max(findPath(node.left), findPath(node.right) );
}
}
and this is my treeNode class definition:
public class TreeNode<T> {
T v;
TreeNode<T> left;
TreeNode<T> right;
TreeNode(T value) //initialize treeNode with treeNode(value)
{
v = value;
left = null;
right = null;
}
}
Upvotes: 2
Views: 1296
Reputation: 1010
If it can help you:
First, rename findPath
to something more appropriate, like longestPathLength
.
Then you can see the 1 +
as a factorization, but it could be non-factorized all the same:
Math.max(1 + findPath(node.left), 1 + findPath(node.right) );
The idea is: among all paths from the root to an end, some pass through the left sub-tree, the others through the right sub-tree.
The length of the longest path passing through the left sub-tree's is: the length of the longest path in the left sub-tree, plus one for the root node that is outside. Idem on the right side.
But ultimately recursion is something you have to grok. It could help seeing it on some simple mathematical examples first, or simpler data structures (like length of a list).
Upvotes: 1
Reputation: 198314
If you are at the bottom, the maximum path to the bottom is zero.
If you are not at the bottom, you can go left or right. Imagine you go left, and see how much farther it is to the bottom. The distance to the bottom on the left is then one more than that (counting the step to the left for the +1
). Then imagine you go right; the distance to the bottom there is again one more than when you measure it from your new position one step to the right. If going left says there's three more steps after going left (four counting the initial step left), and going right says there's six more steps to the bottom (seven with the initial step right), then the longest path to the bottom is seven (the greater of the two).
A
/ \
/ \
B C
/ \ ' `
D E
' ` ' \
F
' `
Say this is our tree. From A
, stepping left, there's 3 more steps to go; stepping right there is 1 more. Thus, total maximum path length from A is 4 (1 + max(3, 1)
).
How do we know there are 3 steps from B? Well, stepping left, there is 1 step from the bottom. And stepping right, there is 2 steps to go. 1 + max(1, 2)
is 3.
Wait, how did we know there was one step to go from D? This is how: stepping left, we're at the bottom (nothing there), so the distance is 0. And stepping right, same thing: 0 again. 1 + max(0, 0)
is 1.
The calculation goes analogously for all the other nodes. All the nodes that were shown with just aborted arcs evaluate to 0 (they are the "terminating condition" of the recursion). All the other nodes will inspect two subtrees and see which one is deeper.
Upvotes: 3