a recorsive method to get level of a certin leaf on a binary tree???
this is a simple recorsive method that print all leaves of a binary tree:
public static void level(Node n)
{
if(n!=null)
{
System.out.print(n.getNumber()+"=>"**********);
level(n.getLeftSon());
level(n.getRightSon());
}
}
now the thing is that next to each leaf (where all the ************ are) i need to print the level of that certin leaf, how the hell do i do that since a regular counter integer wont do (every time ill call the recorsive method it will set it on zero)
i also thought about write another method which give u the level of a certin tree and just place it where needed what i came up with:
private static int inWhichLevel(Node root, Node n)
{
if(root!=null)
{
if(isInTree(root,n))
{
if(isInTree(root.getLeftSon(),n))
{
return inWhichLevel(root.getLeftSon(),n)+1;
}
else
{
return inWhichLevel(root.getRightSon(),n)+1;
}
}
else
return 0;
}
return 0;
}
i cant use it cause as a parameter i need both the root Node and the leaf Node.......
hopefully what i wrote here is understandable.....help would be very very much appreciate......
Re: a recorsive method to get level of a certin leaf on a binary tree???