In this piece of code I am showing all File object stored in linked list. I am running this in recursive way which let me to explore the entire binary tree.
My job is to count on which level of the tree does each File object is located.
How to do this by using recursion? I was thinking of just counting variable inside, but then I don’t know where to subtract the value if level goes down.
Any ideas?
public void display(T node)
{
Node temp = node;
if (temp != null)
{
display(temp.left);
display(temp.right);
}
}
Increment the level at the begining of the function and decrement it at the end, just before the last line
We will increment the level when it enters a new node and decrements it when it leaves the current node and goes to its predecessor.