can anyone please help me with the recorsive code to find the level (height) of a certin leaf in a binery tree??!!
Printable View
can anyone please help me with the recorsive code to find the level (height) of a certin leaf in a binery tree??!!
Sure. What do you have, and where are you stuck?
db
public static void level(Node n)
{
if(n!=null)
{
System.out.print(n.getNumber()+"=>"**********);
level(n.getLeftSon());
level(n.getRightSon());
}
}
thats a simple code of a method that print all leaves in a binary tree what i need to do is next to each number "leaf" print the level where the leaf is located on the tree????
Pass the level as an another parameter and increment it where needed.
kind regards,
Jos
it wont work cause its a recorsive method every time ill call the method it will set my counter integer on zero.....
i also write this method:
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;
}
the thing is that i cant use it cause this method needs both the root Node and the Node which i want to get his level??????!!!!!!
i see what u seeing the thing is that im not allowed to change the signture of the method it has to stay as it is "public static void level(Node n)"
so no room for more parameters.... any other idea??????