can anyone please help me with the recorsive code to find the level (height) of a certin leaf in a binery tree??!!

Printable View

- 02-07-2012, 07:32 AMoshikorenfind the level of a certin leaf in a binery tree???
can anyone please help me with the recorsive code to find the level (height) of a certin leaf in a binery tree??!!

- 02-07-2012, 10:53 AMDarrylBurkeRe: find the level of a certin leaf in a binery tree???
Sure. What do you have, and where are you stuck?

db - 02-07-2012, 05:01 PMoshikorenRe: find the level of a certin leaf in a binery tree???
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???? - 02-07-2012, 05:08 PMJosAHRe: find the level of a certin leaf in a binery tree???
Pass the level as an another parameter and increment it where needed.

kind regards,

Jos - 02-07-2012, 05:34 PMoshikorenRe: find the level of a certin leaf in a binery tree???
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??????!!!!!! - 02-07-2012, 06:16 PMJosAHRe: find the level of a certin leaf in a binery tree???
- 02-07-2012, 06:26 PMNormRe: find the level of a certin leaf in a binery tree???
- 02-07-2012, 06:27 PMoshikorenRe: find the level of a certin leaf in a binery tree???
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?????? - 02-07-2012, 06:40 PMJosAHRe: find the level of a certin leaf in a binery tree???